-1

我正在尝试使用广播按预期工作将父控制器$scope值更新为子控制器$scope值,但在尝试通过发出函数将子$scope 值更新为父值时不更新。$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

4

1 回答 1

0

我正在广播/发射事件,rootscope然后在rootscope.Here is the working plunkr can see the click on this link

var app = angular.module('plunker', []);


app.controller('FirstCtrl', function($rootScope, $scope) {
  $scope.firstRegister = function() {
    $rootScope.$broadcast('broadcast', $scope.name)
  };


  $rootScope.$on('emit', function(events, args) {
    $scope.name = args;
  });

});

app.controller('SecondCtrl', function($rootScope, $scope) {
  $rootScope.$on('broadcast', function(events, args) {
    $scope.name = args;
  });

  $scope.secondRegister = function($event) {
    $rootScope.$emit('emit', $scope.name)
  } ;
});
于 2015-12-07T12:17:20.953 回答