addNewChoice를 클릭했지만 여전히 실패 할 때 필드를 선택 배열에 동적으로 추가하려고합니다.Angular2 동적으로 양식 필드 추가/제거
<form class="form-inline">
<fieldset *ngFor="let choice of choices">
<div class="form-group">
<select class="form-control" id="timeZonePicker">
<option value="-12" >(GMT -12:00) Eniwetok, Kwajalein</option>
<option value="-11" >(GMT -11:00) Midway Island, Samoa</option>
............
</select>
</div>
<div class="form-group">
<input type="time" class="form-control" id="startTimeInput">
</div>
<div class="form-group">
<input class="time" class="form-control" id="endTimeInput">
</div>
</fieldset>
</form>
<button class="pull-left btn btn-success" (click)="addNewItem()">Add Field</button>
<button class="pull-left btn btn-danger" (click)="removeItem()">Remove Field</button>
다음은 구성 요소입니다.
export class TimeZonesComponent {
constructor(){}
choices = [{id: 1}, {id: 2}];
addNewChoice(){
var newItemNo = this.choices.length + 1;
this.choices.push({'id': newItemNo});
}
removeChoice(){
var lastItem = this.choices.length - 1;
this.choices.splice(lastItem);
}}
저는 Angular.js 솔루션을 다양하게 시도했지만 여기에는 Angular2가 없습니다. 어떤 도움을 많이 주시면 감사하겠습니다!
하지만 구성 요소에서 당신은'addNewChoice'와'removeChoice'를 사용? – yurzui
다른 예를보고 우연히 그들이 사용한 이름을 사용했습니다. 적절한 이름으로 변경하면 효과가 있습니다! 엄청 고마워!!! – joaoSaulo
@yurzui 질문이 있습니다. 클래스에서 동적으로 추가 된 양식 필드에 액세스하는 방법 (예 : 위의 예 TimeZonesComponent 클래스) –