[英]how to properly use ngRepeat while using cycle for
I'm trying to color dates in list using this: 我正在尝试使用以下方法为列表中的日期着色:
HTML 的HTML
<div id="list" ng-controller = "listContr as ilist">
<ul>
<li ng-repeat = "item in ilist.items">
<p class="date-{{ilist.dateColor()}}">{{item.dueDate| date: 'dd.MM.yyyy HH:mm'}}</p>
</li>
</ul>
</div>
javascript javascript
app.controller ('listContr', function () {
this.items = tasks;
this.dateColor = function () {
for (var i = 0; i <= tasks[i].length; i++) {
if (currDate > tasks.dueDate) {return 1} esle {return 2};
}
};)};
but it never returns 2 (or dates just colored again in 1). 但它永远不会返回2(或仅将日期重新着色为1)。 I guess that ngRepeat makes items inherit previous properties. 我猜想ngRepeat会使项目继承以前的属性。 So I have no idea how to fix it. 所以我不知道如何解决它。
Change the signature of your function to take the item from ngRepeat, so your function knows which item you are about to color. 更改函数的签名以从ngRepeat中获取项目,以便函数知道要为哪个项目着色。
<div id="list" ng-controller = "listContr as ilist">
<ul>
<li ng-repeat = "item in ilist.items">
<p class="date-{{ilist.dateColor(item)}}">{{item.dueDate| date: 'dd.MM.yyyy HH:mm'}}</p>
</li>
</ul>
</div>
Then you may also get rid of the loop in the function 然后您也可以摆脱函数中的循环
app.controller ('listContr', function () {
this.items = tasks;
this.dateColor = function (item) {
if (currDate > item.dueDate) {return 1} esle {return 2};
};)};
(Note: I have not check this in a browser, just ajusted your code to show the concept) (注意:我没有在浏览器中检查过,只是调整了代码以显示概念)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.