angularjs – angular.js:如何将ngclick从原始dom转移到指令的dom?

前端之家收集整理的这篇文章主要介绍了angularjs – angular.js:如何将ngclick从原始dom转移到指令的dom?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
嗨,我有这个“可确认”按钮指令,我正在处理,

将触发指令’可确认’的HTML代码

<span confirmable ng-click='users.splice($index,1)'></span>

指令:(coffeescript)

angular.module('buttons',[])

  .directive 'confirmable',() ->
    template: """
      <button class='btn btn-mini btn-danger'>
        Destroy
      </button>
    """
    replace: yes

所以我希望通过这个指令生成的最终结果是

<button class='btn btn-mini btn-danger' ng-click='users.splice($index,1)'>
        Destroy
      </button>

到目前为止,我已经在指令中使用了一个链接函数

angular.module('buttons',() ->
    template: """
      <button class='btn btn-mini btn-danger'>
        Destroy
      </button>
    """
    replace: yes
    link: (scope,el,attrs) ->               <---------- linking function
      $(el).attr 'ng-click',attrs.ngClick

但是我再次通过了指令文档,并发现了具有=,@和&操作符,但我真的不确定他们是否是我需要的.那么这是转载属性,我仍然需要理解,但在这一刻似乎也没有帮助.所以当我的链接功能现在的技巧,但我想我应该问,看看角是否提供了一个更优雅的解决方案.

谢谢!

对我来说听起来像你想从你的指令中的父范围中调用一个方法

我已经整理了一个Plunk here

(对不起,我喜欢JavaScript …所以这里)

这是你是父控制器.

app.controller('ParentCtrl',function($scope) {
    $scope.fooCalled = 0;
    $scope.foo = function() {
        $scope.fooCalled++;
    };
});

然后你的标记

<div ng-controller="ParentCtrl">
   Foo Called: {{fooCalled}}<br/>
   <button ng-click="foo()">Call From Parent</button><br/>
   <custom-control custom-click="foo()"></custom-control>
</div>

和你的指示声明:

app.directive('customControl',function(){
   return {
     restrict: 'E',scope: {
        innerFoo: '&customClick'
     },template: '<button ng-click="innerFoo()">Call From Control</button>'
   };
});

指令定义中的范围声明中的位是将父作用域函数引用与指令的范围相关联,以便可以在单击时调用它.那就是&是在那里

原文链接:https://www.f2er.com/angularjs/140678.html

猜你在找的Angularjs相关文章