简体   繁体   English

在Angular 6中使用ngFor循环动态创建输入元素数组并基于模板引用变量添加动态验证

[英]use ngFor loop in Angular 6 to dynamically create array of input elements and add dynamical validation based on template reference variables

I would like to create dynamically 3 input tags in Angular 6 to not copy/paste html code because that input elements have similar html and functionality. 我想在Angular 6中动态创建3个输入标签,以不复制/粘贴html代码,因为这些输入元素具有相似的html和功能。

For this purpose I created an array "reusableItems" inside component and initialize it : 为此,我在组件内部创建了一个数组“ reusableItems”并对其进行了初始化:

    let numberOfInputElements = 3;        
    for (let i = 0; i < numberOfInputElements; i++) {          
      this.reusableItems.push({
        answer: 'Answer ' + (i +1),
        passwordRecoveryAnswer: this.user['passwordRecoveryAnswer' + (i + 1)]
      });
    }

Then I put code inside my html : 然后我将代码放在html中:

<div *ngFor="let item of dropDownDataManagerService.reusableItems" >
  <li class="col-xs-12 pl-lg pr0 pv-sm bd1-bottom">
    <div class="col-xs-4 ph0 pt"> {{item.answerTitle}}</div>
    <div class="col-xs-8">
      <input type="text" name={{item.answer}} ref-{{item.answer}}="ngModel" class="col-sm-12 k-textbox ph0"
             [(ngModel)]=item.passwordRecoveryAnswer
             [pattern]="[a-z]"
             required autocomplete="off"/>

    </div>
  </li>
</div>

It seems works fine but then I need to add error messages when these fields will be empty and not match to pattern. 看来工作正常,但是当这些字段为空且与模式不匹配时,我需要添加错误消息。 Something like : 就像是 :

<div *ngIf="__{{item.answer}}__.errors?.required ">
  {{'Please provide an answer' | translate}}
</div>
<div *ngIf="__{{item.answer}}__.errors?.pattern">
  {{'Pattern is not match'}}
</div>

I don't know what should i put inside ngIf condition. 我不知道应该在ngIf条件中放入什么。 How can I do it if my template reference variables are comes from array? 如果我的模板引用变量来自数组,该怎么办? Is anyone have ideas? 有人有想法吗?

Thanks 谢谢

Angular creates unique template reference variable for each embedded template so that you can use the same template reference variable name inside ngFor loop: Angular为每个嵌入式模板创建唯一的模板参考变量,以便您可以在ngFor循环中使用相同的模板参考变量名称:

<div *ngFor="let item of reusableItems">
    <li class="col-xs-12 pl-lg pr0 pv-sm bd1-bottom">
        <div class="col-xs-4 ph0 pt"> {{item.answerTitle}}</div>
        <div class="col-xs-8">
            <input type="text" name={{item.answer}} ref-answer="ngModel" class="col-sm-12 k-textbox ph0" [(ngModel)]="item.passwordRecoveryAnswer"
             [pattern]="'[a-z]'" required autocomplete="off" />
            <div *ngIf="answer.errors?.required">
                {{'Please provide an answer'}}
            </div>
            <div *ngIf="answer.errors?.pattern">
                {{'Pattern is not match'}}
            </div>
        </div>
    </li>
</div>

In the code above I use the same name for each input in array 在上面的代码中,我对数组中的每个输入使用相同的名称

ref-answer="ngModel" // or you can also use #answer="ngModel

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

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