search

Home  >  Q&A  >  body text

angular.js - angularjs在使用compile的postLink中如何绑定事件?

自定义指令compile在使用时,要如何在postLink中绑定事件呢?

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

在运行时,弹出: iEle.on is not a function,这个错误是什么原因造成的呢?

高洛峰高洛峰2744 days ago547

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