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属性的使用. &, <, =, @ 符 ...
随机推荐
- appium-Could not obtain screenshot: [object Object]
原因 App页面已经被禁止截屏,禁用用户截屏的代码如下: getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE); setConten ...
- linux如何查看端口被哪个进程占用?
参考:https://jingyan.baidu.com/article/546ae1853947b71149f28cb7.html 1.lsof -i:端口号 2.netstat -tunlp|gr ...
- .NetCore下使用Prometheus实现系统监控和警报 (一)介绍【译】
[译]原文https://prometheus.io/docs/introduction/overview 什么是Prometheus? Prometheus是一个开源系统监控和警报工具包,最初起源于 ...
- 195 Tenth Line
Given a text file file.txt, print just the 10th line of the file. Example: Assume that file.txt has ...
- rem_taobaofix.js
https://webresource.cdn.chuma.cn/Library/Rem/rem_taobaofix.js https://webresource.cdn.chuma.cn !func ...
- 001.NFS简介
一 简介 NFS 是Network File System的缩写,即网络文件系统.一种使用于分散式文件系统的协定,由Sun公司开发,于1984年向外公布.功能是通过网络让不同的机器.不同的操作系统能够 ...
- rabbitmq学习(三) —— 工作队列
工作队列,又称任务队列,主要思想是避免立即执行资源密集型任务,并且必须等待完成.相反地,我们进行任务调度,我们将一个任务封装成一个消息,并将其发送到队列.工作进行在后台运行不断的从队列中取出任务然后执 ...
- JDBC之 连接池
JDBC之 连接池 有这样的一种现象: 用java代码操作数据库,需要数据库连接对象,一个用户至少要用到一个连接.现在假设有成千上百万个用户,就要创建十分巨大数量的连接对象,这会使数据库承受极大的压力 ...
- 【FFT&NTT 总结】
$FFT$总结 (因为还不会啊,,都没做过什么题,所以一边学一边打咯.. 1.主要是用来加速卷积形式的求和吧? $F*G(n)=F[i] × G[n-i]$ 平时是$O(n^2)$的,FFT可以$O( ...
- ios网络编程(入门级别)-- 基础知识
在学习ios的过程中,停留在UI控件很长时间,现在正在逐步的接触当中!!!!!!在这个过程中,小编学到了一些关于网络编程知识,并且有感而发,在此分享一下: 关于网络请求的重要性我想不用多说了吧!!!对 ...