angular1.x事件处理方式
-
on()注册一个事件,
-
emit() 调用父级事件,向上传播。
-
broadcast()向下传播事件。
<div ng-app="app" ng-controller="parentCtr">
<div ng-controller="childCtr1">
name : <input ng-model="name" type="text" ng-change="change(name);"/>
</div>
<div ng-controller="childCtr2">
Ctr1 name: <input ng-model="ctr1Name"/>
</div>
</div>
angular.module("app", [])
.controller("parentCtr", function ($scope) {
$scope.$on('parentChange',function (event, msg) {
$scope.$broadcast('ChildCtr1NameChange',msg);
})
})
.controller("childCtr1", function ($scope) {
$scope.change = function (name) {
console.log("childCtr1", name);
$scope.$emit("parentChange", name);
};
})
.controller("childCtr2", function ($scope) {
$scope.$on('ChildCtr1NameChange',function (event, msg) {
$scope.ctr1Name = msg; })
});