【问题标题】:How to keep track of index in Double ngFor loop in angular?如何以角度跟踪 Double ngFor 循环中的索引?
【发布时间】:2020-02-22 16:23:45
【问题描述】:

我正在设计一个反馈页面,其中有多个问题被问到,并且所有问题都有相同的多项选择答案。所以我设计如下。

options: string[] = ['Excellent', 'Fair', 'Good', 'Poor'];

  questions: any = [
    {
      question: "How would you rate A",
      answer: this.options,
      radioSelected: ""
    },
    {
      question: "How would you rate B",
      answer: this.options,
      radioSelected: ""
    },
    {
      question: "How would you rate C",
      answer: this.options,
      radioSelected: ""
    },
    {
      question: "How would you rate D",
      answer: this.options,
      radioSelected: ""
    },
    {
      question: "How would you rate E",
      answer: this.options,
      radioSelected: ""
    }
  ];
<div *ngFor="let q of questions; index as i">
        <label id="example-radio-group-label">{{q.question}}</label>
        <div class="row">
            <div class="form-check" *ngFor="let option of q.answer">
                <div class="col-md-3">
                    <input class="form-check-input" id="{{option}}" [value]="option" type="radio" name="options"
                        [(ngModel)]="q.radioSelected">
                    <label class="form-check-label" for="{{option}}">
                        {{option}}
                    </label>
                </div>
            </div>
        </div>
        <hr>
    </div>

代码在 UI 中显示良好。但是当我单击任何问题的单选按钮时,只会选择第一个问题的选项。我是 Angular 新手,请帮我解决这个问题。

【问题讨论】:

  • 您可以使用第二个索引,例如:*ngFor="let option of q.answer;let j = index"

标签: html angular typescript


【解决方案1】:

您在收音机上缺少唯一名称属性。一组选项的名称应该是唯一的。

在您动态生成单选按钮的用例中,您可以使用循环索引来设置这样的名称 -

 <div *ngFor="let q of questions; let  questionIndex=index">
                    <label id="example-radio-group-label">{{q.question}}</label>
                    <div class="row">
                        <div class="form-check" *ngFor="let option of q.answer; let answerIndex=index">
                            <div class="col-md-3">
                                <input class="form-check-input" [value]="option" type="radio" name="options{{questionIndex}}{{answerIndex}}"
                                    [(ngModel)]="q.radioSelected">
                                <label class="form-check-label">
                                    {{option}}
                                </label>
                            </div>
                        </div>
                    </div>
                    <hr>
                </div>

请检查工作示例here

类似的问题herehere

希望对你有帮助!!

【讨论】:

  • 我试过这个答案,没用。 q.radioSelected 只更新第一个问题
  • @Prodyskumar 请在此处查看工作示例,stackblitz.com/edit/radio-name?file=src/app/app.component.html
  • @shindesan2012,谢谢,我尝试了问题索引而不是答案索引。它工作正常
  • “您在收音机上缺少唯一名称属性”。 @SantoshShinde,不是真的,输入具有name 属性集,但是,是的,正如您稍后提到的,每个部分应该是唯一的。我建议的另一件事是在这种情况下避免使用forid。恕我直言,这是一个必要的复杂性,只需将input 放入label 就足够了,正如seanplwong's answer 中所述。
  • @developer033 是的,我们必须避免在labelinput 中使用forid,我只是尝试让他的代码正常工作并且会得到一些提示。我也在我的答案中提到了同样的事情以及其他答案的链接。
【解决方案2】:

你可能正在渲染类似的东西

<div>Question 1</div>
<div>
  <div>
    <input id="Excellent" ...>
    <label for="Excellent">Ex</label>
  </div>
</div>

<div>Question 2</div>
<div>
  <div>
    <input id="Excellent" ...>
    <label for="Excellent">Ex</label><!-- This is pointing to the first input -->
  </div>
</div>

首先,不要重复id。你可以通过将input 包裹在label 中来避免&lt;label for=""&gt; 的复杂性:

<label><input />Excellent</label>

【讨论】:

  • 当我在 label 中使用 input 时,单选按钮未在 UI 中显示。只有标签可见。 @seanplwong
【解决方案3】:

您可以将答案循环的索引分配给不同的名称,并将该索引与名称值连接起来。试试这个:

<div *ngFor="let q of questions; let  i=index">
    <label id="example-radio-group-label">{{q.question}}</label>
    <div class="row">
        <div class="form-check" *ngFor="let option of q.answer; let answerIndex=index">
            <div class="col-md-3">
                <input class="form-check-input" id="{{option}}" [value]="option" type="radio" name="options{{answerIndex}}"
                    [(ngModel)]="q.radioSelected">
                <label class="form-check-label" for="{{option}}">
                    {{option}}
                </label>
            </div>
        </div>
    </div>
    <hr>
</div>

【讨论】:

  • 我试过这个答案,没用。 q.radioSelected 仅更新第一个问题。在 UI 中,所有复选框都被选中。不知道发生了什么
  • 你能创建一个stackblitz项目吗
猜你喜欢
  • 2023-01-09
  • 1970-01-01
  • 2020-11-23
  • 2017-10-23
  • 1970-01-01
  • 2018-06-28
  • 2019-12-07
  • 1970-01-01
  • 2019-03-16
相关资源
最近更新 更多