AngularJS:将新数据推送到 $scope.myList,顶部 [英] AngularJS: pushing new data to $scope.myList, top
问题描述
当我想操作 DOM 时,向 $scope.myList = []
添加新内容的权利是什么?
What is the right to add new content to $scope.myList = []
, when I want to manipulate the DOM?
我试过 $scope.myList.push(data);
,它在末尾添加新数据,因此在 ng-repeater 内的 HTML 模板的末尾.我想要它在顶部.unshift-function 将重新排列数组并更新所有元素.
I've tried $scope.myList.push(data);
, which adds the new data at the end, and therefore in the end of the HTML-template inside ng-repeater. I want it at the top.
unshift-function will rearrange the array and update all elements.
Angular 新手.
New to angular.
也许问题应该是:我应该如何将新内容动态添加到模板顶部?
Maybe instead the question should be: How should I add new content dynamically into the top of my template?
推荐答案
每当您想添加到数组之前,您都需要重新排列整个数组.如果您可以指定data
的格式,我建议您添加一个entry_number
或id
作为data的字段code> 并使用 angular filter 将新项目放置到顶部.
Whenever you want to prepend to an array, you will be rearranging the entire array. If you can dictate the format of the data
, I suggest you add a entry_number
or id
as a field of the data
and use angular filter to place the new items to the top.
看看这个plunker.
这篇关于AngularJS:将新数据推送到 $scope.myList,顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!