[英]General angular ng-repeat and track by $index
Just a general question about using track by $index in an ng-repeat, I couldn't find a solution in the docs... 只是有关在ng-repeat中使用$ index跟踪的一般问题,我在文档中找不到解决方案...
I have a <div ng-repeat="concert in concerts track by $index">{{concert}}</div>
, below it, I have an array, that is dynamically populated with the concert's start time like this, <p>{{startTime[$index]}}</p>
. 我有一个
<div ng-repeat="concert in concerts track by $index">{{concert}}</div>
在它下面,我有一个数组,即动态地填充与音乐会的开始时间这样的, <p>{{startTime[$index]}}</p>
。 Some concerts, however do not have a valid start time, is there a way to figure out if there is a startTime for that [$index]
and if not, define the text that takes its place? 但是,有些演唱会没有有效的开始时间,是否有办法找出
[$index]
开始时间,如果没有,则定义代替它的文本?
I know this is kind of open ended, & I could use a function to compare the length of concerts
array and startTimes
array, and populate the remaining fields in startTimes
with data, but I was hoping there may be an easier way? 我知道这是一种开放式的,与我可以用一个函数来比较的长度
concerts
阵列和startTimes
阵列,并填充剩余的字段startTimes
有数据,但我希望有可能是一个更简单的方法? Best practice? 最佳实践?
Thanks for your advice! 谢谢你的建议!
you could use an angular filter that will go through and identify if a concert has a start time and set a default value to whatever you want 您可以使用一个角度过滤器,该过滤器将检查并确定音乐会是否有开始时间,并将默认值设置为您想要的任何值
https://docs.angularjs.org/api/ng/filter/filter https://docs.angularjs.org/api/ng/filter/filter
app.filter('startingTimeExists', () => {
return (collection) => {
let filtered = [];
angular.forEach(collection, (concert) =>{
if(concert.hasOwnProperty('startingTime')){
filtered.push(concert);
} else {
concert.startingTime = "7:00PM"
filtered.push(concert)
}
})
return filtered;
};
});
perhaps something like the above will work. 也许上面的东西会起作用。 I haven't tested it.
我还没有测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.