星空网 > 软件开发 > Java

AngularJS中Directive间交互实现合成

 

假设需要烹饪一道菜肴,有3种原料,可以同时使用所有的3种原料,可以使用其中2种,也可以使用其中1种。

如果以Directive的写法,大致是:<bread material1 material2 material3></bread>,或者是<bread material1 material2></bread>...

由此,我们需要自定义一个名称是bread的directive,以元素的方式呈现;需要自定义名称是material1的direcitve,名称是material2的directive,...

我们需要在bread这个directive中首先定义好所有的原料即方法,比如有material1, material2, material3,然后在material1这个directive中需要调用bread这个directive中的material1方法。

这就涉及到了direcitve之间的交互和相互调用了。

如何交互呢?

其实,在direcive中为我们提供了一个require字段,此处用来声明需要调用的外部directive。

假设以这样的发那个是定义一个directive.

app.directive("first", function(){  return {    restrict:"E",    scope:{},//保持独立的scope    controller: function($scope){      $scope.abilities = [];      this.addPower = function(){        $scope.abilities.push("power");      }      this.addVelocity = function(){        $scope.abilities.push("velocity");      }      this.addFly = function(){        $scope.abilities.push("fly");      }    },    link:function(scope, element){      element.bind("mouseenter", function(){        console.log(scope.abilities);      })    }  }})

 


scope字段保证了scope的独立性,并且是以元素形式声明。

然后,分别针对以上first这个directive的3各方法自定义3个directive.

 

app.directive("power", function(){  return{    require:"first",    link: function(scope, element, attrs, first){      first.addPower();    }  }})app.directive("velocity", function(){  return{    require:"first",    link: function(scope, element, attrs, first){      first.addVelocity();    }  }})app.directive("fly", function(){  return{    require:"first",    link: function(scope, element, attrs, first){      first.addFly();    }  }})

 

以上,通过require获取到其它direcitive.

在页面中按如下调用:

<first power velocity fly>Superman</first>

 




原标题:AngularJS中Directive间交互实现合成

关键词:JS

JS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

FastMoss新功能上线!TikTok达人带货数据及联系方式升级,解决分销建联难题:https://www.goluckyvip.com/news/75116.html
英国年度热搜词揭晓,Temu人气最高;eBay英国站物流政策更新:https://www.goluckyvip.com/news/75117.html
事关做东南亚生意的商家,谁先看到谁先赚:https://www.goluckyvip.com/news/75118.html
超60%物流企业竞相出海,能否成功押对筹码?:https://www.goluckyvip.com/news/75119.html
Lazada运营每天必须要做的事情? :https://www.goluckyvip.com/news/7512.html
出海家居品牌「乐歌」拟募资5亿扩建美国海外仓:https://www.goluckyvip.com/news/75120.html
深圳到西安自驾路线攻略 深圳到西安自驾最佳路线:https://www.vstour.cn/a/411228.html
松花蛋是哪里的特产松花蛋的产地:https://www.vstour.cn/a/411229.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流