作用域可以像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;
});
}]);
来源:oschina
链接:https://my.oschina.net/u/2486665/blog/613452