[英]how to update a list inside ng-repeat using angular?
i lave an ionic list 我是一个离子列表
<ion-list>
<ion-item ng-repeat="item in items">
Hello, {{item.name}}!
</ion-item>
</ion-list>
then, in the controller i have a event: 然后,在控制器中我有一个事件:
$scope.items = someData();
itemsRef.on('event', function (response) {
console.log(response); // this is an object containing he "name" property
});
i would like append the response
as a ion-item
, without inserting html, but somehow adding the response to the items 我想将
response
附加为ion-item
,而不插入html,但以某种方式将响应添加到项目中
edit: 编辑:
I've tried: $scope.items.push(response);
我试过了:
$scope.items.push(response);
but weirdly enough i get Uncaught TypeError: undefined is not a function
但奇怪的是我得到了
Uncaught TypeError: undefined is not a function
edit: it looks like one doesn't just $scope.items.push(response);
编辑:它看起来像一个不仅仅是
$scope.items.push(response);
, but $scope.items[next_key_here] = response;
,但是
$scope.items[next_key_here] = response;
If itemRef is an Angular service, all you need is to add the object to the items array: 如果itemRef是Angular服务,您只需将对象添加到items数组:
$scope.items.push( response );
If itemRef uses some non-Angular asynchronous service to get its response, you will need to tell Angular that things have updated: 如果itemRef使用一些非Angular异步服务来获取其响应,则需要告诉Angular事情已更新:
$scope.$apply( function() {
$scope.items.push( response );
});
If you are having problems with $scope, it's always a good idea to use a $scope variable with a dot in, for example use $scope.data.items rather than $scope.items. 如果你遇到$ scope的问题,那么使用带有点的$ scope变量总是一个好主意,例如使用$ scope.data.items而不是$ scope.items。
See http://jimhoskins.com/2012/12/14/nested-scopes-in-angularjs.html for a great discussion on why. 有关原因的详细讨论,请参见http://jimhoskins.com/2012/12/14/nested-scopes-in-angularjs.html 。
I've tried:
$scope.items.push(response);
我试过了:
$scope.items.push(response);
but weirdly enough i getUncaught TypeError: undefined is not a function
但奇怪的是我得到了
Uncaught TypeError: undefined is not a function
That happens because $scope.items isn't an Array
object when you access it before it hasn't been populated with a value yet. 发生这种情况是因为$ scope.items在您尚未填充值之前访问它时不是
Array
对象。 Try doing something like this, which should work: 尝试做这样的事情,这应该工作:
if ($scope.items && $scope.items instanceof Array) {
$scope.items.push(response);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.