【问题标题】:List not refreshing in my angular site在我的角度网站中列表不刷新
【发布时间】:2014-11-23 10:51:59
【问题描述】:

我使用 Ionic 和 AngularJS,但遇到以下问题:

这里是视图:

<ion-list>
    <ion-item ng-repeat="item in items track by $index" >
        <h1>{{item.text}}</h1>
        {{item.timestring}}
    </ion-item>
</ion-list>

和控制器:

$scope.items = foo.getItems();

foo 中的方法如下:

foo.loclStorages = JSON.parse(localStorage.getItem("foo"));
foo.getItems = function () {        
    if (foo.loclStorages === null) {
        return [];
    } else {
        return jsdata.loclStorages.items;
    }
};

现在的问题: 当列表为空并添加第一个元素时,我首先需要更新站点才能看到它。 当我已经在列表中有项目时,我可以在更新后立即看到它。

我需要做什么才能在添加第一个元素后立即看到它?

【问题讨论】:

  • track by $index 在您的 ng-repeat 表达式中做了什么?我建议先看看那里 (see documentation)。

标签: javascript angularjs list ionic-framework


【解决方案1】:

当您的列表为空时,$scope.items 等于某个空数组 []。 如果您的列表不为空,则 $scope.items 是对 jsdata.loclStorages.items 的引用。

由于这两个数组不是同一个引用,所以当您的列表开始为空时,将项目添加到 localStorages.items 将不会影响 $scope.items 中的数组。

要解决这个问题,你需要在添加项时检查作用域中的引用是否与 jsdata 中的数组引用相同。如果没有,请务必再次致电$scope.items = foo.getItems();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-30
    相关资源
    最近更新 更多