在同一页面上使用相同的角度控制器几个位置

时间:2013-06-01 11:23:53

标签: angularjs

我想要一个带有未读消息计数的引导程序导航选项卡。 此外,在消息页面上,我需要显示当前消息并在被要求时删除它们。 问题是看起来第二个div的范围值没有改变。我用演示制作了一个plunker - 问题是第二个实例未按预期更新。我错过了什么?新角度。谢谢。

http://plnkr.co/edit/O2ZduEP5JDkN219jXiNI

1 个答案:

答案 0 :(得分:0)

首先 - 您正在创建两个具有不同范围的AppController

<body ng-controller="AppController"> <!-- here -->
  {{message}}
  <button type="button" ng-click="changeMe()">Click me</button>
  <div ng-controller="AppController"> <!-- and here -->
    <span>Second instance:</span>
    <span>{{message}}</span>
  </div>
</body>

当您单击按钮时,第一个范围中的message将会更新。我不知道这是否是故意的。如果你想要两个范围,你应该使用共享资源并更新它,否则你可以删除第二个控制器。

您的clickMe功能中也有错误:

$scope.changeMe = function() {
  $scope.message = "changed";
  // $scope.changeMe is triggered from ng-click, which is wrapped in an $apply
  // the following line will there cause an "Error: $apply already in progress" exception
  $scope.$apply();
};
相关问题