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; })
});
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者