加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 09:28:22 所属栏目:安全 来源:网络整理
导读:嗨,我有这个“可确认”按钮指令,我正在处理, 将触发指令’可确认’的html代码 span confirmable ng-click='users.splice($index,1)'/span 指令:(coffeescript) angular.module('buttons',[]) .directive 'confirmable',() - template: """ button class='bt
嗨,我有这个“可确认”按钮指令,我正在处理,

将触发指令’可确认’的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>'
   };
});

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读