How to make the angle recognize an ng-keyup event created dynamically in the element

1

I'm creating a directive that should dynamically include a function in the ng-keyup angular event, I tried to do this:

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 angle does not execute the function. When I manually include the attribute in html in this way everything works.

<input type="text" ng-model="ngModel" ng-keyup="validador()" />

According to the colleague's response, the DOM was already rendered when my directive includes the attribute dynamically, so the attribute exists in the element, but the angle does not recognize it.

    
asked by anonymous 23.04.2018 / 20:26

2 answers

1

This is because it has already redressed the final html. You could try to cast instead of ng-keyup adding the DOM event onkeyup this way:

function link(scope, element, attrs, ngModel) {
    if (scope.validator !== undefined) {
        element[0].onkeyup = validador();
    }     
}
    
23.04.2018 / 20:35
1

You can use jquery to get by placing the code below inside ngOnInit ()

import * as $ from 'jquery';   
ngOnInit(){
 $(document).keyup(function(r){
      /* Fazer ação */
    })
}
    
03.05.2018 / 22:26