【问题标题】:ng-repeat showing wrong values in ionic 1ng-repeat 在离子 1 中显示错误值
【发布时间】:2018-08-01 23:06:42
【问题描述】:
<ion-item
  class="item-icon-left item-icon-right item-avatar"
  ng-repeat="note in taskDetails.notes track by note.note_id"
  ng-click="viewNote(note)">
  <i ng-show="note.status.name==Open" class="icon ion-ios-circle-outline"></i>
  <i ng-show="note.status.name!=Open"class="icon ion-ios-checkmark-outline"></i>
  <h2>{{note.note}}</h2>
  <p>{{note.created_by}}</p>
  <i class="icon ion-chevron-right icon-accessory"></i>
</ion-item>

任务对象

{"task_date_id":69069,"task_id":"3286","start_time":"2018-02-22 12:10:00","end_time":"2018-02-22 12:10:00 ","overdue_days":0,"subject":"Oneone","description":"One","status":{"id":3,"name":"Pending","enabled":null,"完成":null},"statuses":[{"id":3,"name":"Pending"},{"id":6,"name":"Assigned"},{"id":4, "name":"Closed"},{"id":17,"name":"Complete"}],"type":{"id":229,"name":"200_Test_Mobile_Task"},"assigned_to": "Kanishka Raveendra","address":"","clients":[],"attachments":[],"notes":[],"$$hashKey":"object:8112"}

笔记对象

{"note_id":"98834","note_date":"22/02/2018","note_type":"ABC14","time_spent":0,"note":"Xyxhxffuf\n

{$PERSON_FIRST_NAME} {$PERSON_SURNAME}

","created_by":"Kanishka Raveendra","date_created":"","attachments":[],"template":"

{$PERSON_FIRST_NAME} {$PERSON_SURNAME}

","type":{"id":52,"name":"ABC14","enabled":true,"record_time":"start_stop"}}

这里我添加了用于显示数据的Task和Note的源代码和响应。通常一个任务可以有多个notes。但是在这里我们可以清楚地看到notes数组没有元素。但是我可以看到一些notes视图中其他任务的对象。我需要在视图中显示正确的注释值。请帮助我。我是 ionic 新手。

控制台错误

错误:[ngRepeat:dupes] 不允许在转发器中重复。使用“track by”表达式指定唯一键。中继器:taskDetails.notes 中的注释由 note.note_id 跟踪,重复键:98834,重复值:{"note_id":"98834","note_date":"22/02/2018","note_type":"ABC14", "time_spent":0,"note":"Xyxhxffuf\n

{$PERSON_FIRST_NAME} {$PERSON_SURNAME}

","created_by":"Kanishka Raveendra","date_created":"","attachments":[],"template":"

{$ PERSON_FIRST_NAME} {$PERSON_SURNAME}

","type":{"id":52,"name":"ABC14","enabled":true,"record_time":"start_stop"}}

【问题讨论】:

  • 问题是什么?
  • 没有注释值的视图显示一些注释。任务对象中的注释数组为空
  • 如果没有注释,则为空。请解释您期望的行为。定义“正确”并解释当前行为如何不“正确”。
  • 在“note.note_id 跟踪 taskDetails.notes 中的注释”中,taskDetails 存在,但在 JSON 中 taskDetail 字段在哪里..?
  • 清除您的计算机

标签: angularjs cordova ionic-framework angularjs-ng-repeat


【解决方案1】:

在您的track by note.note_id 中获得相同的值。

使用$index,它是ng-repeat中重复元素(0..length-1)的迭代器偏移量。

更多详情请查看here

使用以下解决方案:

      <ion-item
        class="item-icon-left item-icon-right item-avatar"
        ng-repeat="note in taskDetails.notes track by $index"
        ng-click="viewNote(note)">
        <i class="icon" ng-class="{'ion-ios-circle-outline': note.status.name === 'Open', 'ion-ios-checkmark-outline': note.status.name !== 'Open'}"></i>

        <h2>{{note.note}}</h2>
        <p>{{note.created_by}}</p>
        <i class="icon ion-chevron-right icon-accessory"></i>
      </ion-item>

希望对你有帮助!!

【讨论】:

  • 嗨 Santhosh,$index 是什么?
  • 重复元素的迭代器偏移量(0..length-1)
  • 在我的代码中添加“taskDetails.notes track by $index 中的注释”可以吗
  • 是的,请检查它,然后您的控制台上没有错误
  • 好的也可以用ng-class代替多个ng-show来应用动态类
【解决方案2】:

试试这个

<ion-item
        class="item-icon-left item-icon-right item-avatar"
        ng-repeat="(key, value) in taskDetails.notes track by $index"
        ng-click="viewNote(note)">
        ......
</ion-item>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-06
    • 1970-01-01
    • 1970-01-01
    • 2017-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多