How to access a dom element in a Angular directive

前端 未结 2 603
甜味超标
甜味超标 2020-12-14 16:25

I am trying to attach a keyup event to a directive in my Angular project. Here is the directive:

angular.module(\'clinicalApp\').directive(\'chatContainer\'         


        
相关标签:
2条回答
  • 2020-12-14 17:04

    You can easily do it with Angular' element' find() method:

     var chatbox = elem.find("textarea");  // Finding
     chatbox.bind("keyup",function(){      // Binding
         console.log("KEYUP!")
     })
    

    Live example: http://jsfiddle.net/cherniv/S7XdK/

    0 讨论(0)
  • 2020-12-14 17:20

    You can use element.find(yourSelector) as previously mentioned, but it is better to use ngKeyUp, similar to how you would use ngClick:

    https://docs.angularjs.org/api/ng/directive/ngKeyup

    0 讨论(0)
提交回复
热议问题