【问题标题】:Angular 4 ngModel binding within nested *ngFor嵌套 *ngFor 中的 Angular 4 ngModel 绑定
【发布时间】:2018-11-11 20:17:33
【问题描述】:

您好,我遇到了一个问题,我无法将数据绑定到您将在此嵌套 *ngFor 中看到的“选定”值。这里发生的事情是我有一个可能的害虫(虫子等)的列表,对于每个害虫我都有一个我必须问这个人的问题列表。因此,当我遍历每个选定的害虫,然后进入每个问题(某些害虫有相同的问题)时,它会将一种害虫的答案与具有相同问题的所有其他害虫联系起来。无论我尝试什么,都不允许我将选定的答案绑定到特定的害虫而不是剩余的害虫。这是我的 HTML:

这是我正在努力工作的工作的 Stackblitz。就像在 stackblitz 中一样,我有一系列问题要推到各种害虫中。

STACKBLITZ ANGULAR APP

  <div *ngFor="let each of selectedPestProblems; let x = index;">
    <div *ngIf="selectedPest === x" class="pestQuestions">
      <div *ngFor="let questions of each.question; let i = index; trackBy:trackByIndex">
        <h4 *ngIf="i == 0" style="padding-left: 15px;"><br>{{each.pest}}</h4>
        <label>{{questions.question}}</label>
        {{x + " " + i}}
        <div *ngIf="questions.type == 'checkbox'" (click)="changeCheckBox2(x, i)">
          <input type="checkbox" class="css-checkbox" [(ngModel)]="selectedPestProblems[x].question[i].selected" name="checkbox{{i + '' + x}}">
          <label class="css-label"> - {{questions.title}}</label>
        </div>
        <div *ngIf="questions.type == 'select'">
          <select class="otherSelects" [(ngModel)]="selectedPestProblems[x].question[i].selected" name="select{{i + '' + x}}">
            <option *ngFor="let answers of questions.answer" [value]="answers.id">
              {{answers.name}}
            </option>
          </select>
        </div>
        <div *ngIf="questions.type == 'selectOther'">
          <select class="otherSelects" [(ngModel)]="selectedPestProblems[x].question[i].selected" name="selectOther{{i + '' + x}}">
            <option *ngFor="let answers of questions.answer" [value]="answers.id">
              {{answers.name}}
            </option>
          </select>
          <div *ngIf="questions.selected == 5">
            <br>
            <textarea [(ngModel)]="selectedPestProblems[x].question[i].description" placeholder="Tell Us Where It Is Located." name="description{{i + '' + x}}"></textarea>
          </div>
        </div>
        <br>
        <div *ngIf="i == selectedPestProblems[x].question.length - 1">
          <button (click)="removePestProblem(x)" style="margin-left: 15px;">Remove Pest Problem</button>
          <br>
        </div>
      </div>
    </div>
  </div>

那么这里是我循环遍历的数组的示例。

所以,这个数组只显示了我正在循环的一种害虫,我在底部有另一个害虫“蜜蜂”,发生的是我的复选框和选择框,我认为它应该在哪里被建模为每个害虫的​​每个问题的“选定”值,但是当我为其中一个选择答案时,它表明该答案不是为具有相同问题的所有其他害虫选择的。

【问题讨论】:

  • 您可能会发现在 StackBlitz 中更容易重现
  • 我会试一试,如果我成功了,我会把它添加到帖子中。感谢您的建议

标签: angular typescript angular-ngmodel


【解决方案1】:

别忘了,JavaScript/TypeScript 对象是引用类型。您将同一个问题对象推送到两个不同的数组中,这并不意味着两者不同 - 两个对象都相同 - 就内存位置而言,它们的位置是相同的。

您可以使用 TypeScript 的 Object Spread 运算符使对象彼此不同 - 它们是不同的(不同的内存位置)。

ngOnInit(){
    for(var i = 0; i < this.selectedPestProblems.length; i++){
      this.selectedPestProblems[i].question.push({...this.question1});
      this.selectedPestProblems[i].question.push({...this.question2});
    }
  }

[只是一个旁注,它是一个不同的级别]

我已经为您的 stackblitz 代码提供了一个解决方案。 https://stackblitz.com/edit/angular-rphm3z?file=src/app/app.component.ts

【讨论】:

  • 非常感谢!!你刚刚节省了我很多时间尝试将几乎所有害虫都不同的一系列问题添加到近 150 种害虫中。你摇滚。
  • 能否请你给我一个链接,它教给我传播运算符,我以前从未见过这个,我很惊讶地看到我需要解决这个问题的只是合法的 5 个字符在我的代码中。
  • 您可以使用Object.assign({}, this.question1)。等同于{...this.question1}....alligator.io/typescript/object-rest-spread & blog.mariusschulz.com/2016/12/23/…
猜你喜欢
  • 2016-07-27
  • 2019-05-06
  • 2017-03-11
  • 2017-12-06
  • 2017-12-05
  • 2018-01-01
  • 2023-03-27
相关资源
最近更新 更多