[英]ng-repeat not working
I have a data structure that is generated by a function. 我有一个函数生成的数据结构。 It returns the the last 30 days in reverse order (as in 11, 10, 9, 8 etc.) as an array of objects.
它以对象数组的相反顺序(例如11、10、9、8等)返回最近30天。
function HoursCtrl($scope) {
$scope.days = function () {
var d = new Date();
var result = [];
for (i = 0; i < 31; i++) {
result.unshift({day:d.getDate() });
d.setDate(d.getDate() - 1);
}
return result;
};
};
Here is the output of the function: 这是函数的输出:
[ { day: 9 }, { day: 8 }, { day: 7 }, { day: 6 }, { day: 5 }, etc. ]
I am using ng-repeat to expose this data as a list: 我正在使用ng-repeat将此数据公开为列表:
<div ng-controller="HoursCtrl">
<ul>
<li ng-repeat="day in days">{{day.day}}</li>
</ul>
</div>
The li elements are not being generated! li元素未生成! What am I missing?
我想念什么?
您需要将其作为函数来调用。
ng-repeat="day in days()"
Instead of setting up a function as a method on the scope, just populate a days
property on the scope: 无需在范围上将函数设置为方法,只需在范围上填充
days
属性:
function HoursCtrl($scope) {
var d = new Date();
$scope.days = [];
for (var i = 0; i < 31; i++) {
$scope.days.unshift({day:d.getDate() });
d.setDate(d.getDate() - 1);
}
};
For better performance, you might try looping in reverse and using push
instead of unshift
: 为了获得更好的性能,您可以尝试反向循环并使用
push
而不是unshift
:
function HoursCtrl($scope) {
var d = new Date();
var counter = 31;
var date = d.getDate();
$scope.days = [];
d.setDate(date - counter + 1);
while ( counter-- ) {
date = d.getDate();
$scope.days.push({ day: date });
d.setDate(date + 1);
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.