如何使用 Angular 更新 NG-Repeat 中的列表
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>
然后,在控制器中我有一个事件:
$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;
见JSFIDDLE
如果 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);
}
相关文章:
- 将我的列表 vom ng-repeat 更改为收集重复 ng-click 后,无法正常工作
- AngularJS Dependent下拉列表,带ng repeat
- 使用ng repeat对列表进行分页
- 如何在angularjs中使用ng-repeat时将当前月份保持在列表顶部
- 根据本地存储中的列表过滤 ng-repeat
- AngularJS ng-repeat列表是动态构建的,$index始终为零
- AngularJs:我想从数组或列表中添加一个类,用于ng-repeat中的每次迭代
- 在 ng-repeat 中添加和删除 JSON 列表
- 添加重复键对象时如何更新 ng-repeat 列表中的元素
- 复选框列表 将数组格式化为 JSON 并在 ng-repeat 中映射后未填充
- Angular - ng-repeat - 获取 1 个列表中的所有 json 子数据
- AngularJs:选择在使用ng-repeat时不显示任何选项的下拉列表
- AngularJS嵌套ul列表和ng-repeat
- angularjs ng-repeat 中的动态互斥下拉列表
- 如何使用 Angular 更新 NG-Repeat 中的列表
- jQuery .focus() 无法使用 ng-repeat (AngularJS) 处理动态生成的列表项
- 推送到数组不会通过ng repeat指令更新视图列表
- 在视图中使用compile in指令和ng repeat的组合会阻止正确更新范围元素列表
- 在<ul>ng repeat生成的列表
- 如何使用ng repeat以缩略图格式创建一行中有n个项目的m个项目的列表