[英]how to update a list inside ng-repeat using angular?
我是一個離子列表
<ion-list>
<ion-item ng-repeat="item in items">
Hello, {{item.name}}!
</ion-item>
</ion-list>
然后,在控制器中我有一個事件:
$scope.items = someData();
itemsRef.on('event', function (response) {
console.log(response); // this is an object containing he "name" property
});
我想將response
附加為ion-item
,而不插入html,但以某種方式將響應添加到項目中
編輯:
我試過了: $scope.items.push(response);
但奇怪的是我得到了Uncaught TypeError: undefined is not a function
編輯:它看起來像一個不僅僅是$scope.items.push(response);
,但是$scope.items[next_key_here] = response;
如果itemRef是Angular服務,您只需將對象添加到items數組:
$scope.items.push( response );
如果itemRef使用一些非Angular異步服務來獲取其響應,則需要告訴Angular事情已更新:
$scope.$apply( function() {
$scope.items.push( response );
});
如果你遇到$ scope的問題,那么使用帶有點的$ scope變量總是一個好主意,例如使用$ scope.data.items而不是$ scope.items。
有關原因的詳細討論,請參見http://jimhoskins.com/2012/12/14/nested-scopes-in-angularjs.html 。
我試過了:
$scope.items.push(response);
但奇怪的是我得到了Uncaught TypeError: undefined is not a function
發生這種情況是因為$ scope.items在您尚未填充值之前訪問它時不是Array
對象。 嘗試做這樣的事情,這應該工作:
if ($scope.items && $scope.items instanceof Array) {
$scope.items.push(response);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.