简体   繁体   English

如何使用角度指令创建点击事件?

[英]How to create click event using angular directive?

I am very new to creating AngularJs directive , so below i have created directive and when user click on delete button i am checking what are the values of scope,element and attrs but its not printing anything in console. 我是创建AngularJs指令的新手,因此下面创建了指令,当用户单击Delete按钮时,我正在检查scope,element和attrs的值是什么,但在控制台中未打印任何内容。 ProcessDTO is the json data in the controller. ProcessDTO是控制器中的json数据。

Any idea what i am missing here, i have read the directive documentation but if someone can put some light and explain it as beginner level it would be great. 不知道我在这里缺少什么,我已经阅读了指令文档,但是如果有人可以轻描淡写并将其解释为初学者水平,那就太好了。

html html

<button class="btn btn-danger"
        type="button" autodelete delete-tags="processDTO">Delete</button>

directive.js 指令

angular.module('App').directive('autoDelete', function () {
    'use strict';
    return{
      restrict:'A',
      scope:{
      autoDeleteTags: '=deleteTags'
      },
      link:function(scope,element,attr){
        $(element).click(function(){
          console.log('Element',element);
          console.log('SCOPE',scope);
          console.log('ATTRS',attr);
        })
      }
  }

});

The directive named autoDelete is looking for attribute auto-delete not autodelete 名为autoDelete的指令正在查找属性auto-delete而不是autodelete

Try: 尝试:

<button class="btn btn-danger"
        type="button" auto-delete delete-tags="processDTO">Delete</button>

Also would suggest using ng-click instead of creating your own event handlers 也建议使用ng-click而不是创建自己的事件处理程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM