您的位置:首页 > Web前端 > AngularJS

angularJs controller间怎么通信

2016-02-03 00:00 826 查看
摘要: 可利用作用域的事件传播 事件:广播/冒泡(发射)事件

作用域可以像DOM节点一样,进行事件的传播。主要是有两个方法:

broadcasted :从父级作用域广播至子级 scope

emitted :从子级作用域往上发射到父级作用域

下面是代码案例

<div class="container" ng-controller="parentCtrl">

<div class="row" ng-controller="childCtrl">
<input type="text" ng-model="cv" ng-change="change(cv)">
</div>
<div class="row" ng-controller="childCtrl2">
{{ cv2 }}
</div>

</div>

'use strict';
angular.module('app', [])
.controller('parentCtrl', ['$scope', function ($scope) {
$scope.$on('childCtrlChange', function (event, msg) {
$scope.$broadcast('childAll', msg);
});
}])
.controller('childCtrl', ['$scope', function ($scope) {
$scope.change = function (msg) {
$scope.$emit('childCtrlChange', msg);
};
}])
.controller('childCtrl2', ['$scope', function ($scope) {
$scope.$on('childAll', function (event, msg) {
$scope.cv2 = msg;
});
}]);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息