2014-11-23 27 views
0

不刷新我用液鹼中和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; 
    } 
}; 

現在的問題: 當列表爲空且我添加第一個元素時,我首先需要更新站點才能看到它。 當我已經有列表中的項目時,我可以在更新後立即看到它。

我需要做的,也看到它添加的第一個元素後瞬間是什麼?

+0

在你的'ng-repeat'表達式中'$ by index'是什麼?我建議先看看那裏([見文檔](https://docs.angularjs.org/api/ng/directive/ngRepeat))。 – hon2a 2014-11-23 11:02:53

回答

0

當你的列表是空的,那麼$ scope.items等於一些空數組[]。 當你的列表不爲空時,那麼$ scope.items是對jsdata.loclStorages.items的引用。

由於這兩個數組不是相同的,那麼當你的列表開始爲空,將項目添加到localStorages.items將在$ scope.items陣列上沒有任何影響。

爲了解決這個問題,你需要檢查,當你添加一個項目,如果在範圍內引用是同一基準在jsdata數組。如果沒有,請務必再次致電$scope.items = foo.getItems();