controller 和 指令 通讯方法
在 angular 中我们经常会使用多个 controller 和 指令
他们拥有各自的 $scope , 这就产生了跨$scope调用的问题。
有几种常见的方法来可以使用.
方法一 : 指令 require
<div directive1="xx">
<div directive2></div>
</div> directive("directive1", [function () {
return {
restrict: "A",
link: function () { },
scope: true,
controller: ["$scope", function ($scope) {
this.alert = function () {
$scope.name = "100"
}
}],
name: "directive1Controller"
}
}]).
directive("directive2", [function () {
return {
restrict: "E",
require: "^directive1Controller", //调用父级指令的controller
link: function (scope, elem, attrs, directive1Controller) {
directive1Controller.alert(); //使用方法
}
}
}]).
指令通过require,调用其它指令controller方法, 达到通讯
方法二 : service
service("sharing", [function () {
this.data = "";
}]).
controller("ctrl", ["$scope", "sharing", function ($scope, sharing) {
$scope.updateData = function () {
sharing.data = "new value";
}
}]).
directive("directive1", ["sharing", function (sharing) {
return {
restrict: "A",
link: function (scope) {
scope.$watch(function () {
return sharing.data;
}, function () {
scope.name = sharing.data;
});
}
}
}]).
模块间如果要通讯最好使用 service 来提供接口 , 那service 和 controller 间可以通过 $watch 来更新$scope.
$watch 有些缺点,内存消耗多
方法3 :事件
controller("ctrl", ["$scope", "sharing", "$rootScope", function ($scope, sharing, $rootScope) {
$scope.updateData = function () {
$rootScope.$broadcast("sharingChange", "new value");
//$emit 是向上冒泡广播
//$broadcast 是向下广播
}
}]).
directive("directive1", ["sharing", function (sharing) {
return {
restrict: "A",
link: function (scope) {
scope.$on("sharingChange", function (e, newValue) {
scope.name = newValue;
});
}
}
}]).
这是比较官方的做法。
总结 :
子层和父层通讯,多使用继承的$scope, 指令的 require ,事件广播
模块间的通讯使用service提供接口会比较容易看的明白, service 和 controller 指令间的通讯,可以用watch,$on或者全局变量(模块内的全局,别用太多既可)
service 接口虽然好, 不过由于指令复用性很高,如果每个操作都开接口的话,很快接口就会很多,所以要确保接口是复用性高的,如果只是为了某次开发为配合某模块而开就不值得了。
$rootScope.$broadcast("Main.myParent.alert", function ($scope) { //某个模块, (通过传入操作方法,这里直接写操作$scope)
$scope.name = "keatkeat"
});
$scope.$on("Main.myParent.alert", function (e, fn) { //常用指令
fn($scope); //调用操作方法并把$scope传入,让外部的逻辑实现操作$scope
});
我们可以把复用性不高的操作,写在外面,这样就可以不用写太多的接口了。
controller 和 指令 通讯方法的更多相关文章
- SpringMVC实现一个controller写多个方法
MultiActionController与ParameterMethodNameResolver在一个Controller类中定义多个方法,并根据使用者的请求来执行当中的某个方法,相当于Struts ...
- MVC路由规则以及前后台获取Action、Controller、ID名方法
1.前后台获取Action.Controller.ID名方法 前台页面:ViewContext.RouteData.Values["Action"].ToString(); Vie ...
- Xshell同时向多个会话发送指令的方法
我们平时使用XSHELL.SecureCRT.putty等ssh连接工具连接到远程主机,每次输入指令都是在单一会话窗口,如果有很多台会话,需要同时输入同样的指令,我们就不用一一输入,浪费时间和精力.可 ...
- 控制器controller与指令中的link、controller中变量作用域的关系
angjualrjs中的作用域与原生js中的函数嵌套原理一致,都是存在作用域的继承.若在子控制器(同样包括在指令中的link或是controllerding中定义变量,此时指令中必须未使用scope独 ...
- MVC前后台获取Action、Controller、ID名方法 以及 路由规则
前后台获取Action.Controller.ID名方法 前台页面:ViewContext.RouteData.Values["Action"].ToString();//获取Ac ...
- jfinal的controller默认访问的方法是什么
index()方法: 如: @Controller("/test/exam")public class TestController 如下请求请求:http://localhost ...
- SDI011 读卡器自动发送00A4选择指令 解决方法
如标题,SDI读卡器会自动发送 004A的应用选择指令 解决方法: 是Certificate Propagation 服务 弄的, 关闭就好了
- Java线程通讯方法之wait()、nofity() 详解
Java线程通讯方法之wait().nofity() 详解 本文将探讨以下问题: synchronized 代码块使用 notify()与notifyAll()的区别 Java wait(),noti ...
- Android USB Host 与 HID 之通讯方法
Android USB Host与HID通讯,就目前Google Developer提供的方法有bulkTransfer()与controlTransfer(),看是简简单单的两个方法,要实现真正的通 ...
随机推荐
- 【HDOJ】1057 A New Growth Industry
纯粹的模拟题目. #include <cstdio> #include <cstring> #include <cstdlib> #define MAXN 20 # ...
- Android数据存储三剑客——SharedPreferences、File、SQLite
Android中常用的数据存储一般有三种方式:SharedPreferences.文件和SQLite数据库,用来保存需要长时间保存的数据.本文将通过几个具体的小实例来讲解这三种方式的具体实现. 数据存 ...
- jQuery/CSS3实现图片层叠展开特效
这是一款基于jQuery和CSS3的图片层叠展开特效,让鼠标滑过图片时即可触发这些特效.其中有一款就像扇子展开收拢一样,看起来效果都非常不错.当然本文主要还是来分析一下用jQuery实现这一效果的方法 ...
- Vim--写在开始
这是Vim的学习记录,结合Vim使用技巧一书.
- 扩展Visual Studio IDE
安装visual studio 2012 SDK 下载visual studio SDK. 安装可能遇到的问题 安装时报错:Visual Studio 2012 Install Fails: Prog ...
- angular 数据加载动画 longding
由于公司服务器架构不行,每次加载数据都要很久,但是都是使用angular来渲染数据,不像jquery有beforsend什么的方法, 这是一个github上找的,很轻使用也很简单 1.安装 npm ...
- Baidu与Google地图API初探
前天周六,有个好友过来玩,他说想在他的站点中加入地图导航模块,但不知道选择哪个第三方Map API 在网上查了下Baidu.Google.QQ和MapBar等4种Map API(都是採用JS开放API ...
- Qt 域名转成IP
#include <stdio.h>#include <stdlib.h>#include <netdb.h>#include <arpa/inet.h> ...
- VirtualBox虚拟机网络设置
VirtualBox虚拟机网络设置 测试环境:物理机win10企业版本,VirtaulBox版本5.0.14,虚拟机安装Windows XP及linux系统 想实现虚拟机上网的最简单方式,修改虚拟机网 ...
- 浏览器格式化JSON输出,thinkphp
1 //编写类方法用$this->ajaxReturn()返回数据 2 public function index(){ 3 $user = M('User'); 5 $data = $user ...