我正在尝试使用广播按预期工作将父控制器$scope
值更新为子控制器$scope
值,但在尝试通过发出函数将子$scop
e 值更新为父值时不更新。$scope
这是我的代码:
app.controller('FirstCtrl', function($rootScope, $scope) {
$scope.firstRegister = function() {
$rootScope.$broadcast('broadcast', $scope.name)
};
$scope.$on('emit', function(events, args) {
$scope.name = args;
});
});
app.controller('SecondCtrl', function($rootScope, $scope) {
$scope.$on('broadcast', function(events, args) {
$scope.name = args;
});
$scope.secondRegister = function() {
$rootScope.$emit('emit', $scope.name)
};
<div ng-controller="FirstCtrl" class="ng-scope">
<input ng-model="name" />
<button ng-click="firstRegister()">Parent</button>
<div ng-controller="SecondCtrl" class="ng-scope">
<input ng-model="name" />
<button ng-click="secondRegister()">Child</button>
</div>
</div>
plnkr 链接:http ://plnkr.co/edit/l9qD0ZO4MptnWx6u3U9c?p=preview