1
I’m creating a directive that should dynamically include a function in the angular event ng-keyup
, I tried to do it this way:
Directive
angular.module('app').directive('validar',validar);
validar.$inject = [];
function validar(){
var diretica = {
restrict: 'E',
require: '?ngModel',
scope: {
validador: '&'
},
templateUtl: '....html template segue abaixo',[
link: link
};
return diretiva;
function link(scope, element, attrs, ngModel) {
if (scope.validator !== undefined) {
element[0].setAttribute("ng-keyup", "validador()");
}
}
}
HTML
<input type="text" ng-model="ngModel" />
Problem
I was able to create the attribute in the element, but the angular does not execute the function. When I include the attribute manually in html this way everything works.
<input type="text" ng-model="ngModel" ng-keyup="validador()" />
According to the Honourable Member’s reply, the DOM has already been rendered when my directive dynamically includes the attribute, so the attribute exists in the element, but the angular does not recognize it.
tried using $Scope. $apply() to try to force the angler to update the gift and link the props?
– Caio Koiti
@Caiokoiti not yet, I’ll try to do it, thank you!
– Rodrigo K.B