简体   繁体   English

如何使用angular5在ngFor中绑定ngModel

[英]how to bind ngModel in ngFor using angular5

Cananyone please help. Cananyone请帮忙。 I am using ngFor to iterate through an array and I need to bind the headings to ngModel. 我正在使用ngFor遍历数组,并且需要将标题绑定到ngModel。 How many headings i have entered, that all headings i tested by giving an alert in ts file. 我输入了多少个标题,通过在ts文件中提供警报来测试了所有标题。 It gives an empty alert. 它给出一个空警报。

https://stackblitz.com/edit/angular-gzcy61?file=src%2Fapp%2Fapp.component.css https://stackblitz.com/edit/angular-gzcy61?file=src%2Fapp%2Fapp.component.css

Thanks in advance. 提前致谢。

You are not storing the input value in arrayOfObj; 您没有将输入值存储在arrayOfObj中;

Just click the 'Add new heading button" and enter whatever you want in the <input> block; replace your code in stackblitz with the following HTML and TS. 只需单击“添加新标题按钮”,然后在<input>块中<input> ;使用以下HTML和TS替换stackblitz中的代码。

app.component.html app.component.html

 <input type="text" [(ngModel)]="head1" placeholder="enter text"> <ng-template #temp> <div *ngFor="let a of arrayOfObj"> <input class="form-control" id="usr" placeholder="Enter heading..." [(ngModel)]="a.head2"> <br> <br> <div class="col-md-2"> <div class="btn-div"> <div class="ss" (click)="onAddRow()" *ngIf="tmpo!=2;else tempo">Add new heading {{a.count}}</div> </div> </div> </div> </ng-template> <div class="col-md-2"> <div class="btn-div"> <div class="ss" (click)="onAddRow()" *ngIf="tmpo!=1;else temp">Add new heading</div> <input type="button" class="ss" value="submit" (click)="submit()"> </div> </div> 

app.component.ts app.component.ts

    import { Component } from '@angular/core';
import { FormGroup, FormBuilder, Validators, FormControl, FormArray, NgForm } from '@angular/forms'

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  head1: string = '';
  head2: string = '';
  tmpo;
  count = 0;
  arrayOfObj: any[];

  constructor() {
    this.arrayOfObj = [];
  }

  onAddRow() {
    this.tmpo = 1;
    this.count++;
    this.arrayOfObj.push({ "count": this.count, "head2": "" });
  }

  onAddRow1() {
    // this.tmpo=2;
  }

  submit() {
    console.log(this.arrayOfObj);
    for (var i=0; i<this.arrayOfObj.length; i++){
      alert(this.arrayOfObj[i].head2);
    }
    //alert(this.head1)
    //alert(this.head2)
  }

}

I think you misunderstood on how the binding works and about how you bind it. 我认为您对绑定的工作方式以及绑定的方式误解了。

Even structurally, you are not doing it right in terms of HTML, or maybe I just don't understand what are you trying to do. 即使从结构上来讲,您在HTML方面的工作方式也不对,或者我只是不明白您要做什么。

For your case, at the moment, the array is just a normal array, you need to turn it into array of object, such as: 对于您的情况,目前,该数组只是一个普通数组,您需要将其转换为对象数组,例如:

export class AppComponent {
  head1: string = '';
  head2: string = '';
  tmpo;
  count = 0;
  arrayOfObj: {head: string; position: number}[] = [];

onAddRow(){
this.tmpo=1;
this.count ++;
this.arrayOfObj.push({head: '', position: this.count});
}

Then your HTML can be: 然后,您的HTML可以是:

<ng-template #temp>
  {{arrayOfObj|json}}
  <div *ngFor="let a of arrayOfObj; let i = index">
      <input class="form-control" id="usr" placeholder="Enter text..." [(ngModel)]="arrayOfObj[i].head"> 
        <br>
        <br>   

        <div class="col-md-2">
            <div class="btn-div">
              <div class="ss" (click)="onAddRow()" *ngIf="tmpo!=2;else tempo">Add new heading {{a.position}}</div>
            </div>
        </div>
  </div>
</ng-template>

ngModel does work in for loop, but you need to reference it by the array via index. ngModel确实可以在for循环中使用,但是您需要通过数组通过索引来引用它。

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

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