假设我已经实现了如下指令:
module.directive("foo", function($scope) {
return {
controllerAs: "model",
controller: function($scope) {
this.items = $scope.items;
this.selectItem = function(item) {
this.selectedItem = item;
};
},
scope: { items: "=" }
};
});
module.directive("bar", function($scope) {
return {
controllerAs: "model",
controller: function() {
this.item = $scope.item;
},
scope: { item: "=" }
};
});
...其中foo指令的模板如下所示:
<bar item="model.selectedItem" />
<ol>
<li ng-repeat="item in model.items" ng-click="model.selectItem(item)">
<bar item="item" />
</li>
</ol>
...,bar
指令的模板如下所示:
<span ng-bind="item.text"></span>
当model.selectedItem
由于用户单击某些重复而进行 更改时<bar />
,外部<bar />
不知道任何所谓的属性更改。也就是说,外部<bar />
不会更新其boundmodel.text
属性。
我无法弄清楚为什么foo
控制器上的模型更改不会更改外部bar
指令。
selectedItem
生活在foo
指令范围内。因此,它对外部不可见(该bar
指令)。
您可以保留另一个包含所选项目的键的变量。然后,可以在bar
伪指令中按如下所示绑定值:
<bar item ="items[selectedKey].text"></bar>
编辑:您也可以尝试在范围而不是上设置值this
。
$scope.selectedItem = item;
代替
this.selectedItem = item;
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句