简体   繁体   English

在使用cycle进行循环时如何正确使用ngRepeat

[英]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.

相关问题 如何正确使用AngularJS摘要周期 - How to properly use the AngularJS digest cycle 如何在开放层上使用ngRepeat - How to Use ngRepeat with Open Layers 在角度中,如何在使用过滤器显示的字段上将orderBy与ngRepeat结合使用,以更改其实际内容? - In angular, how to use orderBy with ngRepeat on fields that are displayed using filters, changing their actual content? 如何在与NextGen兼容的同时使用Cycle2? - How to use Cycle2 while keeping compatible with NextGen? 如何使用transclude将ngRepeat“模板”传递给ngDirective? - How to pass ngRepeat “template” to a ngDirective using transclude? 如何将Kendo-grid用于Angular-ngRepeat - How to use Kendo-grid into Angular-ngRepeat 如何在函数返回的元素上使用ngRepeat(实际上是一个过滤器) - AngularJS? - How to use ngRepeat on element returned from a function (a filter, actually) - AngularJS? AngularJS:如何将ngRepeat指令与数组和子数组一起使用? - AngularJS: How to use the ngRepeat directive together with arrays and subarrays? 结合使用ngIf和ngRepeat - using ngIf with ngRepeat 使用Socket IO从数据库获取数据时使用React的哪种生命周期方法 - Which Life Cycle method to use of React while getting data from Database using Socket IO
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM