angular transfer msg from one controller to the other controller by $scope.$on()
来源:互联网 发布:深圳关键词排名软件 编辑:程序博客网 时间:2024/05/15 17:05
<!--test.htm-->
<html ng-app="app">
<body>
<div ng-controller="ControllerOne">
<span>ControllerOne</span> <input ng-model="message" >
<button ng-click="handleClick(message);">BROADCAST</button>
</div>
<div ng-controller="ControllerTwo">
<span>ControllerTwo</span>
<input ng-model="message" >
</div>
<div >
<span>my-component</span>
<my-component ng-model="message"></my-component>
</div>
</body>
<script>
var app = angular.module('app', []);
app.factory('sharedService', function($rootScope) {
var sharedService = {};
sharedService.message = '';
sharedService.prepForBroadcast = function(msg) {
this.message = msg;
this.broadcastItem();
};
sharedService.broadcastItem = function() {
$rootScope.$broadcast('myEvent');
};
return sharedService;
});
app.controller('ControllerOne', ['$scope', 'sharedService',ControllerOne]);
app.controller('ControllerTwo', ['$scope', 'sharedService',ControllerTwo]);
function ControllerOne($scope, sharedService) {
$scope.handleClick = function(msg) {
sharedService.prepForBroadcast(msg);
};
$scope.$on('myEvent', function() {
$scope.message = 'ONE: ' + sharedService.message;
});
}
function ControllerTwo($scope, sharedService) {
$scope.$on('myEvent', function() {
$scope.message = 'TWO: ' + sharedService.message;
});
}
app.directive('myComponent', function(sharedService) {
return {
restrict: 'E',
controller: function($scope, $attrs, sharedService) {
$scope.$on('myEvent', function() {
$scope.message = 'Directive: ' + sharedService.message
});
},
replace: true,
template: '<input>'
};
});
</script>
</html>
- angular transfer msg from one controller to the other controller by $scope.$on()
- angular controller as syntax vs scope
- angular controller as syntax vs scope
- mysql query to find the maximum 3 values from a column and group by on other column
- From one Activity To other Activity
- Transfer Statistics from one Database to Another
- Angular find controller from element or no element
- angular controller的使用
- Angular.js 指令 & Controller
- angular controller 依赖声明
- angular controller不执行
- 测试angular中的controller
- angular controller不起作用解决方案
- angular controller错误
- angular--控制器controller
- Angular学习-controller
- Angular传多个值到controller
- angular controller as
- 哈希表
- Visual Studio 2010的调试(断点设置、变量查看)
- oracle获取表空间文件大小
- WireShark 过滤语法
- IOS 文件目录及NSUserDefaults浅谈
- angular transfer msg from one controller to the other controller by $scope.$on()
- ArithUtil
- PB得到本机的名称和IP地址MAC地址
- POJ 1789
- 慢慢聊异步IO之Linux Epoll
- 菜鸟WDF驱动开发系列(1):VM+Windbg调试平台的搭建
- Spark and Tez Highlight MapReduce Problems
- Unity3D教程宝典之 FX Maker
- 【OpenCV文档】用于角点检测的Fast算法