Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了angularjs – Angular指令如何添加一个属性到元素?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想知道如何做的工作这个片段:
//html
<div ng-app="app">
<div ng-controller="AppCtrl">
    <a my-dir ng-repeat="user in users">{{user.namE}}</a>
</div>
</div>

//js
var app = angular.module('app',[]);
            app.controller("AppCtrl",@R_616_1013@) {
                $scope.users = [{name:'John',id:1},{name:'anonymous'}];
                $scope.fxn = function() {
                    alert('It works');
                };

            })  
            app.directive("myDir",function ($compilE) {
                return {
                 link:function(scope,el){
                      el.attr('ng-click','fxn()');
                      //$compile(el)(scopE); with this the script go mad 
                  }
                };
            });

我知道这是关于编译阶段
但我没有得到点,所以一个简短的解释是
非常感谢。
提前致谢。

将另一个伪指令添加到同一元素的伪指令:

类似答案:

> How to get ng-class with $dirty working in a directive?
> creating a new directive with angularjs

这是一个plunker:http://plnkr.co/edit/ziU8d826WF6SwQllHHQq?p=preview

app.directive("myDir",function($compilE) {
  return {
    priority:1001,// compiles first
    terminal:true,// prevent lower priority directives to compile after it
    compile: function(el) {
      el.removeAttr('my-dir'); // necessary to avoid infinite compile loop
      el.attr('ng-click','fxn()');
      var fn = $compile(el);
      return function(scopE){
        fn(scopE);
      };
    }
  };
});

更清洁的解决方案 – 不使用Ng点击所有:

管道:http://plnkr.co/edit/jY10enUVm31BwvLkDIAO?p=preview

app.directive("myDir",function($parsE) {
  return {
    compile: function(tElm,tAttrs){
      var exp = $parse('fxn()');
      return function (scope,elm){
        elm.bind('click',function(){
          exp(scopE);
        });  
      };
    }
  };
});

大佬总结

以上是大佬教程为你收集整理的angularjs – Angular指令如何添加一个属性到元素?全部内容,希望文章能够帮你解决angularjs – Angular指令如何添加一个属性到元素?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。