简体   繁体   English

如何在* ngFor中设置唯一的模板引用变量? (角度)

[英]How to set unique template reference variables inside an *ngFor? (Angular)

I have a bunch of input fields in a *ngFor loop. 我在*ngFor循环中有一堆输入字段。 The documentation says that template reference variables should be unique. 该文档说模板引用变量应该是唯一的。 Is there a way to do something like #attendee-{{person.id}} to make it unique? 有没有办法像#attendee-{{person.id}}这样使其独特?

   <div *ngFor="let person of attendeesList">
       <input #attendee [ngModel]="person.name" (blur)="changeName(attendee.value)"/>
   </div>

(I know that there is the option of doing (ngModelChange)="changeName($event)" but there are reasons I need to use blur instead. Specifically, I don't want the model to change until the person is done typing the name and we have validated that the name is not empty and not a duplicate name. (我知道可以选择执行(ngModelChange)="changeName($event)"但出于某些原因,我需要使用blur方法 。具体来说,我希望在完成此操作之前键入要更改的模型名称,并且我们已经确认该名称不为空,并且不是重复的名称。

Your template reference variable is already unique because you use it inside embedded view scope: 您的模板参考变量已经是唯一的,因为您可以在嵌入式视图范围内使用它:

<div *ngFor="let person of attendeesList">
  <input #attendee [ngModel]="person.name" (blur)="person.name = attendee.value"/>
</div>

Working Example 工作实例

But you can even omit template reference variable as shown below: 但是您甚至可以省略模板引用变量,如下所示:

<div *ngFor="let person of attendeesList">
  <input [ngModel]="person.name" (blur)="person.name = $event.target.value"/>
</div>

Template variables need to be unique with respect to the template rather than the rendering of the template (the actual values). 模板变量相对于模板必须是唯一的,而不是模板的呈现(实际值)。 You want to use ViewChildren . 您想使用ViewChildren

@ViewChildren('attendee') attendeeInputs: QueryList<ElementRef>;

You can treat attendeeInputs as a QueryList and operate on each attendee input individually as needed based on iteration of the indices. 你可以把attendeeInputs作为QueryList以及基于指数的迭代需要在每个与会者输入单独操作。

If you have an iterable series of Inputs in your form and a decent amount of logic regarding how you want the form to respond to user input the ReactiveFormsModule is going to be a much better fit. 如果您的表单中有一系列可迭代的Inputs(输入),以及关于您希望表单如何响应用户输入的大量逻辑,那么ReactiveFormsModule合适。 This way you don't have to worry about template reference variables and can interact with the FormControls directly. 这样,您不必担心模板引用变量,并且可以直接与FormControls交互。

Essentially with Reactive Forms it would look something like 本质上,使用反应形式,它看起来像

Component 零件

// Initialize your Form
initForm(): void {
    this.Form = this._FormBuilder.group({
        arrayOfInputs: this._FormBuilder.array(initArray())
    })
}

// Initialize the Array of Inputs 
initArray(): FormGroup[] {
    return [
        { this._FormBuilder.group({ inputValue: [''] }),
        { this._FormBuilder.group({ inputValue: [''] }),
        { this._FormBuilder.group({ inputValue: [''] })
    ]
}

Template 模板

<ng-container formArrayName="formArray">
    <div [formGroupName]="i" *ngFor="let Inputs of Form.get('arrayOfInputs').controls; let i=index">
        <input formControlName="inputValue" type="text">
    </div>
</ng-container>

There's a few things missing here, but generally this is how I build forms that have interable inputs. 这里缺少一些东西,但是通常这就是我构建具有可交互输入的表单的方式。 The major difference is that usually when initializing the FormArray, I am using data pulled from somewhere. 主要区别在于通常在初始化FormArray时,我会使用从某处提取的数据。 It really helps to build your forms in logical chunks. 这确实有助于以逻辑块形式构建表单。

Reactive Forms 反应形式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM