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间交互实现合成的更多相关文章
- angularJS中directive父子组件的数据交互
angularJS中directive父子组件的数据交互 1. 使用共享 scope 的时候,可以直接从父 scope 中共享属性.使用隔离 scope 的时候,无法从父 scope 中共享属性.在 ...
- angularJS中directive与controller之间的通信
当我们在angularJS中自定义了directive之后需要和controller进行通讯的时候,是怎么样进行通讯呢? 这里介绍3种angular自定义directive与controller通信的 ...
- AngularJS中Scope间通讯Demo
在AngularJS中,每一个controller都有对应的Scope,而Scope间有时候需要通讯.比如有如下的一个controller嵌套: <body ng-controller=&quo ...
- angularjs中directive指令与component组件有什么区别?
壹 ❀ 引 我在前面花了两篇博客分别系统化介绍了angularjs中的directive指令与component组件,当然directive也能实现组件这点毋庸置疑.在了解完两者后,即便我们知道co ...
- angularjs学习之六(angularjs中directive指令的一般编程事件绑定 模板使用等)
angular js 中模板的使用.事件绑定以及指令与指令之间的交互 相应教学视频地址(需FQ):v=aG8VD0KvUw4">angularjs教学视频 <!doctype h ...
- angularJS中directive与directive 之间的通信
上一篇讲了directive与controller之间的通信:但是我们directive与directive之间的通信呢? 当我们两个directive嵌套使用的时候怎么保证子directive不会被 ...
- AngularJS中Directive指令系列 - scope属性的使用
文章是转的,我做下补充.原文地址:https://segmentfault.com/a/1190000002773689 每当一个指令被创建的时候,都会有这样一个选择,是继承自己的父作用域(一般是外部 ...
- AngularJS中Directive指令系列 - 基本用法
参考: https://docs.angularjs.org/api/ng/service/$compile http://www.zouyesheng.com/angular.html Direct ...
- AngularJS中Directive指令系列
近段时间在研究Angular中的directive用法,打算写个系列.以官方文档为主.并参考诸多教程.加上自己的思考. 基本概念及用法 scope属性的使用. &, <, =, @ 符 ...
随机推荐
- js自定制周期函数
function mySetInterval(fn, milliSec,count){ function interval(){ if(typeof count==='undefined'||coun ...
- 用一句SQL查询相对复杂的统计报表
--统计从2017年3月份开始每个月金融服务支付前分期申请数以及通过(核账完成)数 ,ApplyTime)) ,ApplyTime)) as varchar)+'月' as 日期,count(*) a ...
- springcloud使用Zuul构建微服务网关入门
为什么要使用微服务网关 不同的微服务一般会经过不同的网络地址,而外部客户端可能需要调用多个服务的接口才能完成一个业务需求. 如果让客户端直接与各个微服务通信,会有以下的问题: 客户端会多次请求不同的微 ...
- 启动tomcat时报错Several ports (8005, 8080, 8009) required by Tomcat v5.5 Server at localhost are already in use.
[报错] Several ports (8005, 8080, 8009) required by Tomcat v5.5 Server at localhost are already in use ...
- 浅谈js设计模式之迭代器模式
迭代器模式无非就是循环访问聚合对象中的各个元素.比如 jQuery中的 $.each 函数,其中回调函数中的参数 i 为当前索引, n 为当前元素,代码如下: $.each([1, 2, 3], fu ...
- SOA并不能解决高并发事务
传统SOA架构其实无法面对高并发事务. 这种方式不适合热点资源,也就是高并发场合. 虽然乐观锁短,但是容易产生脏数据. SOA是以服务这个方式对外提供功能,我们很显然喜欢在Service中加上JTA等 ...
- k8s的imagePullSecrets如何生成及使用
如果公司的docker仓库(harbor),需要用户认证之后,才能拉取镜像. 那如何在k8s里生成这个secret呢? 这个secret如何还原呢? 在k8s的yaml文件里如何实现呢? 这里提供几个 ...
- 事务ACID特性
所谓事务,它是一个操作序列,这些操作要么都执行,要么都不执行,它是一个不可分割的工作单位.例如,银行转帐工作:从一个帐号扣款并使另一个帐号增款,这两个操作要么都执行,要么都不执行. 数据库事务必须具备 ...
- js下判断 iframe 是否加载完成的完美方法
一般来说,我们判断 iframe 是否加载完成其实与 判断JavaScript 文件是否加载完成. 采用的方法很类似: var iframe = document.createElement( ...
- laravel Tinker报错 BadMethodCallException with message 'Call to undefined method Illuminate\Database\Query\Builder
进行模型关联操作, php artisan tinker 执行 $user = App\Models\User::find(1) $user->followings()->attach([ ...