小编Pra*_*dar的帖子

AngularJS控制器继承

AngularJS具有基于DOM的控制器继承,如角度文档中所述.

<div ng-controller="BaseController">
    <p>Base Controller Value: {{value}}</p>
    <button ng-click="updateValue()">Update In Base</button>
    <div ng-controller="DerivedController">
        <p>Derived Controller Value: {{value}}</p>
        <button ng-click="updateValue()">Update In Derived</button>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

范围变量"value"仅出现在BaseController中.基于此,在BaseController或DerivedController中更改方法的值时,我希望两个值都需要更新.但只有单个范围变量才会更新.

这是一个演示相同示例的小提琴:http://jsfiddle.net/6df6S/1/

如何在一个级别上进行更改以传播到当前作用域的直接子级和直接父级.

我们可以通过使用实现这一点

$范围.$腕表

没有使用它或任何这样的观察者,有没有办法做到这一点?

编辑1:

通过使用$ scope.$ watch这就是我的意思

$scope.$watch("value", function () {
   $scope.$broadcast("childChangeListener"); //Downwards to all children scopes
   $scope.$emit("parentChangeListener"); //Upwards to all parent scopes
});
Run Code Online (Sandbox Code Playgroud)

我一直在寻找在不使用这种机制的情况下在所有范围内更新值的方法.

angularjs angularjs-scope

28
推荐指数
4
解决办法
3万
查看次数

标签 统计

angularjs ×1

angularjs-scope ×1