2016-09-24 3 views
1

[ngModel]을 사용하여 바인딩하는 동안 ngFor의 입력 유형 = "text"에서 값을 가져 오는 데 문제가 있습니다. ngFor에서 모든 입력을 묶을 수 있습니까?입력 유형에서 입력 값을 얻는 방법 = "text"in loop ngFor

HTML

<button (click)="saveFeature()" type="button">save</button> 

<input class="form-control" type="text" /> 
<button (click)="addFeature()" type="button">Add</button> 

<div *ngFor="let inputservice of servicesfeature_id; let i=index"> 
    <input class="form-control" [(ngModel)]="listServiceFeature" type="text" /> 
    <button (click)="RemoveFeature(i)" type="button">Remove</button> 
</div> 

구성 요소

servicesfeature_id: any = []; 
servicesfeature_length: number = 0; 
listServiceFeature: any = []; 
servicefeature_name: string; 

saveFeature(): void { 
    console.log(this.listServiceFeature); 
} 

addFeature(): void { 
    this.servicesfeature_id.push('service' + this.servicesfeature_length); 
    this.servicesfeature_length += 1; 
} 

RemoveFeature(index): void { 
    this.servicesfeature_length -= 1; 
    this.servicesfeature_id.splice(index, 1); 
} 

여기에 내가 이것을 이해한다면, 당신은 입력이 listServiceFeature 배열의 구성원에 결합해야 할 코드 plnkr.co

답변

2

입니다. 그게 맞습니까? 그게 당신이 원하는 무엇을 경우 인덱스를 사용하여 배열 구성원에 직접 바인딩 할 수 있습니다 : 이제

<input class="form-control" [(ngModel)]="listServiceFeature[i]" type="text" /> 

를 사용하면 추가 입력에 텍스트를 추가하고 콘솔에서 전체 배열을 얻을 저장 명중합니다.

관련 문제