angularjs 不同的controller之间值的传递
Sharing data between controllers in AngularJS
I wrote this article to show how it can possible to pass data from one Controller to another one.
There are two ways to do it, using a service or exploiting depending parent/child relation between controller scopes.
In this post, we’ll analyze the last one method.
It is possible to send data from parent controller to a child controller and viceversa.
To transmit data from the FirstController to SecondController, which the scope of the first one is parent to the scope of the second one, it should use $broadcast method in the FirstController.
Here the javascript code:父传(
$scope.
$broadcast)子接收($scope.
$on)
angular.module('myApp', [])
.controller('ParentCtrl', ['$scope', function($scope) {
$scope.message = "Child updated from parent controller";
$scope.clickFunction = function() {
$scope.$broadcast('update_parent_controller', $scope.message);
};
}
])
.controller('ChildCtrl', ['$scope', function($scope) {
$scope.message = "Some text in child controller";
$scope.$on("update_parent_controller", function(event, message) {
$scope.message = message;
});
}
]);
Here a plunker for a live demo.
Instead, if it need to send data from the SecondController (child) to the FirstController (parent), it should use the $emit method.
Here the javascript code:子传(
$scope.
$emit)父接收($scope.
$on)
angular.module('myApp', [])
.controller('ParentCtrl', ['$scope', function ($scope) {
$scope.message = "Some text in parent";
$scope.$on("update_parent_controller", function(event, message){
$scope.message = message;
});
}])
.controller('ChildCtrl', ['$scope', function ($scope) {
$scope.clickFunction = function() {
$scope.message = "Parent updated";
$scope.$emit('update_parent_controller', $scope.message);
}
}]);
Here a plunker for a live demo.
Finally, here a little trick where two controller have no parent/child relationship.
It should pass data from one controller through $rootScope and the $broadcast method.
Here the javascript code:兄弟传($rootScope.$broadcast)兄弟接收($rootScope.$on)
angular.module('myApp', [])
.controller('FirstCtrl', ['$scope', '$rootScope', function($scope, $rootScope) {
$scope.message = "Clicked!";
$rootScope.clickFunction = function() {
$rootScope.$broadcast("Update", $scope.message);
};
}])
.controller('SecondCtrl', ['$scope','$rootScope', function($scope,$rootScope) {
$scope.message = "Waiting for a click...";
$rootScope.$on("Update", function(event, message) {
$scope.message = message;
});
}]);
Here a plunker for a live demo.
以上三个Demo 打不开,需要FQ。
angularjs 不同的controller之间值的传递的更多相关文章
- [转]ASP.NET MVC中的两个Action之间值的传递--TempData
本文转自:ASP.NET MVC中的两个Action之间值的传递--TempData 一. ASP.NET MVC中的TempData 在ASP.NET MVC框架的ControllerBase中存在 ...
- DDX和DDV——控件与变量之间值的传递
DoDataExchange由框架调用,作用是交互并且验证对话框数据,主要由(DDX) 和 (DDV)宏实现. 永远不要直接调用这个函数,而是通过UpdateData(TRUE/FALSE)实现控件与 ...
- AngularJS实战之Controller之间的通信
我们时常会在不同controller之间进行通信,接下来就介绍三种controller之间的通信方式 一.使用$on.$emit和$broadcast进行controller通信 虽然AngularJ ...
- ASP.NET MVC中的两个Action之间值的传递--TempData
一. ASP.NET MVC中的TempData 在ASP.NET MVC框架的ControllerBase中存在一个叫做TempData的Property,它的类型为TempDataDictiona ...
- Angular中Controller之间的信息传递(第二种办法):$emit,$broadcast,$on
$emit只能向parent controller传递event与data( $emit(name, args) ) $broadcast只能向child controller传递event与data ...
- Iframe之间及iframe与父窗体之间值的传递
方法一:ScriptManager.RegisterClientScriptBlock(this,typeof(Page), "NoInformation", "wind ...
- MVC进阶学习--View和Controller之间的数据传递(二)
1. 使用Request.Form MVC 将页面简单化,与WebForm中的事件机制完全不同,就和普通的html标签表单提交没有任何区别(当然WebForm中的事件机制其实也是表单提交).在表单提交 ...
- MVC进阶学习--View和Controller之间的数据传递(一)
1.使用ViewData ViewData 的是ControllerBase 的一个属性,是一个数据字典类型的,其实现代码如(这段代码来自asp.net MVC开源项目中源码)下: Code 1 ...
- 通过$broadcast或$emit在子级和父级controller之间进行值传递
通过$broadcast或$emit在controller之间进行值传递,不过这些controller必须是子级或者父级关系, $emit只能向父级parent controller传递事件event ...
随机推荐
- apache 访问权限基本设置
1 .禁止访问某个或多个文件夹 在.htaccess文件里面写入 RewriteRule ^foldername - [F,L] #禁止访问某个文件夹 RewriteRule ^ ...
- bzoj 1877: [SDOI2009]晨跑
#include<cstdio> #include<iostream> #include<cstring> #define M 6009 #define inf 2 ...
- bzoj 1791: [Ioi2008]Island 岛屿
#include<iostream> #include<cstdio> #define M 1000009 using namespace std; *M],cnt,n,hea ...
- 合理利用 vs2013的性能分析跟诊断
选择对应的项目==> 我正常是选择采样 就包括里面的一些耗时. 挺好用的. 可以根据热路径 还有访问的占比.知道哪个环节占用的访问时间 还有性能耗能多. 可以点进去 跟踪跟修改
- 动态加载DLL函数GetProcAddress错误
GetLastError获取错误代码127,指代“找不到指定程序”. 解决: 转自:http://hi.baidu.com/violetwangy/item/c35b3b95ecf5374cf0421 ...
- Android MotionEvent getX() getY() getRawX() getRawY() and View getTop() getLeft()
getRowX:触摸点相对于屏幕的坐标getX: 触摸点相对于按钮的坐标getTop: 按钮左上角相对于父view(LinerLayout)的y坐标getLeft: 按钮左上角相对于父view(Lin ...
- Ch2.Making Reconmmendation in PCI
做<Programing Collective Intelligence>中chapter 2.Making Recommendation的实例,有3个问题花了好长时间: 1. 遇到报错& ...
- js中的apply调用
今天看了阮一锋老师的一篇文章,感觉很明了对闭包的理解,尤其是文章中的apply的介绍 apply()是函数对象的一个方法,它的作用是改变函数的调用对象,它的第一个参数就表示改变后的调用这个函数的对象. ...
- Discuz 插件制作之后台常用函数详解
目录 showsetting()表单显示 cpmsg()提示消息 showformheader()创建表单头 showformfooter()创建表单尾 showtableheader()创建表格头 ...
- OD调试程序3
条件跳转指令的图片,作为以后的参考. 载入了reverses.eve程序,F8下去,发现一个跳转,调用了一个函数,致使程序结束.于是我们绕过它,参考上面的 跳转指令图片. 然后继续F8 又会发现一个跳 ...