控制器属性更改不会影响某个指令上的整个绑定属性

MatíasFidemraizer

假设我已经实现了如下指令:

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>

TL; DR:我的问题

model.selectedItem由于用户单击某些重复而进行 更改时<bar />,外部<bar />不知道任何所谓的属性更改。也就是说,外部<bar />不会更新其boundmodel.text属性。

我无法弄清楚为什么foo控制器上的模型更改不会更改外部bar指令。

nipuna777

selectedItem生活在foo指令范围内。因此,它对外部不可见(该bar指令)。

您可以保留另一个包含所选项目的键的变量。然后,可以在bar伪指令中按如下所示绑定值:

<bar item ="items[selectedKey].text"></bar>

编辑:您也可以尝试在范围而不是上设置值this

$scope.selectedItem = item;

代替

this.selectedItem = item; 

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章