Angularjs 在模型更新后不更新 ng-repeat 的视图

卡尔斯

我有一个“菜单指令”,​​它拒绝在模型更改时更新 html 视图中的项目。我正在使用带有 for-each 语句的 angular js。这是我将其精简为的最低代码。

该指令:

export function SideNavbarDirective() {
    'ngInject';

    let directive = {
        restrict: 'E',
        templateUrl: 'app/components/sideNavbar/sideNavbar.html',
        scope: {
        },
        controller: SideNavbarController,
        controllerAs: 'vm',
        bindToController: true
    };

    return directive;
}

class SideNavbarController {
    constructor($scope, $timeout) {
        'ngInject';

        $scope.myItems = [
            {
                displayName:"1",
                highlight:true,
            },
            {
                displayName: "2",
                highlight: false,
            },
            {
                displayName: "3",
                highlight: false,
            },
        ];

        $timeout(() => {
            $scope.myItems[0].highlight = false;
            $scope.myItems[1].highlight = true;
            $scope.myItems.length = 2;
            $scope.$apply()
        }, 4000);

    }
}

html:

<div ng-repeat="item in myItems" layout="row">
    <div flex layout="column">
            <div layout="row">
                <span>{{item.displayName | translate}}</span><span flex=""></span><span ng-show="{{item.highlight}}">*</span>
            </div>
    </div>
</div>

初始视图:

1* 
2
3

定时器后:

1* 
2

定时器后的预期结果:

1 
2*

正如你可以看到删除一个项目反映。但是更改数组中对象的内容不会被 angular 注意到。我究竟做错了什么?

编辑:我更新了 HTML 以仅打印 highlight 属性的值。

<div ng-repeat="item in myItems" layout="row">
    <div flex layout="column">
            <div layout="row">
                <span>{{item.displayName | translate}}</span><span flex=""></span><span>{{item.highlight}}</span>
            </div>
    </div>
</div>

现在我得到: 初始视图:

1 true
2 false
3 false

定时器后:

1 false
2 true

所以,现在我得到了预期值。卧槽!问题一定出在 ng-show 表达式上还是什么?我比以往任何时候都更加困惑。

xReeQz

中不需要花括号ng-show,所以应该是ng-show="item.highlight"

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

数组更新后AngularJS ng-repeat不更新

带有外部图像源的AngularJS ng-repeat不更新图像视图

更改模型后ng-repeat不更新

AngujarJs-ng-repeat + instance。$ remove不更新视图

当模型更改时,Angularjs ng-repeat不会更新

插入数据库后AngularJS ng-repeat不更新

为什么AngularJS ng-bind不更新视图

AngularJs ng-repeat with track未更新

AngularJS:在视图中执行ng-repeat后无法访问范围变量

更改模型后AngularJS视图未更新

在范围中更新ng-repeat循环数组后,视图未更新

$ scope更新后,ng-repeat不更新信息

在内部更改Notification Factory模型后,不更新Angular ng-repeat

通过(key,val)在ng-repeat中的Angularjs ng-model不更新

AngularJS:从模型数组中拼接模型元素时,不会更新ng-repeat列表

ng-repeat angularjs 中的值更改后,ng-class 未更新

AngularJS不按ng-repeat排序

Angularjs-为什么我可以从另一个视图更新对象,但不能更新列表(ng-repeat)?

拼接后角度ng-repeat不更新

AngularJS-ng-repeat,更新尚未定义的模型对象索引

ng-repeat不更新数组更新

将动态ng模型转换为ng-repeat AngularJS

AngularJS:元素的属性更改时强制ng-repeat更新

AngularJS通过输入更新ng-repeat不起作用?

AngularJS:指令更新ng-repeat内的父范围

使用 angularJS 和 PHP 更新 ng-repeat 的特定值

更新AngularJS中ng-repeat表中的数组项

使用AngularJS和Firebase更新ng-repeat中的值

使用来自 firestore 的数据更新 angularjs ng-repeat