search

Home  >  Q&A  >  body text

angular.js - How to bind events in angularjs postLink using compile?

When using the custom instruction compile, how to bind events in postLink?

compile:function(tElement,tAttrs,transclude){
    return {
         post:function postLink(scope,iEle,iAttrs,controller){
            iEle.on('click',function(){
                alert(1);
            }  
          }            
    }

When running, the following pops up: iEle.on is not a function, what is the cause of this error?

高洛峰高洛峰2782 days ago581

reply all(1)I'll reply

  • 大家讲道理

    大家讲道理2017-05-15 17:06:54

    You wrote it wrong, there is a missing bracket when registering the click event

    compile:function(tElement,tAttrs,transclude){
        return {
             post:function postLink(scope,iEle,iAttrs,controller){
                iEle.on('click',function(){
                    alert(1);
                });  // 这里少个小括号
              }            
        }

    https://jsfiddle.net/hjzheng/...

    reply
    0
  • Cancelreply