Вставить массив в formArray - PullRequest
       86

Вставить массив в formArray

0 голосов
/ 26 октября 2019

Я создаю два поля динамически, одно из них - поле выбора, а другое - p-чипы (точно так же, как теги), теперь я хочу вставить все эти теги с идентификатором блока выбора в formArray, если я передам массив, возвращенный p-микросхемы при создании полей, затем пропускает первую запись, потому что массив в это время пуст. Пожалуйста, проверьте imgs и код для лучшего понимания. enter image description here enter image description here

component.html

 <label>
        <div *ngFor="let t of a.controls; let i = index"  class="field-heading">Attribute (e.g. Colour)
      <div [formGroup]="t" >
        <select formControlName="attribute"  class="attr-dropdown">
          <option *ngFor="let value of attribute" [ngValue]="value.id">
            {{ value.name }}
          </option>
        </select>     
      </div> 
      <div class="flex-one">
        <label>
          <div class="field-heading">Value (e.g. Red, Blue, Green)</div>

          <p-chips inputStyleClass="full-width theme-input" (onAdd)="myfunc($event)"></p-chips>
        </label>
      </div>
      </div>

      </label>

<div class="add-variantes-row">

  <a >
    <div class="add-variant-btn"> + </div>
    <div class="ml-2 pointer" (click)="addAttitubute()">Add attribute</div>
  </a>

</div>

component.ts

 ngOnInit() {
    this.dynamicForm = this.formBuilder.group({
        attributes: new FormArray([])
    });
  }
get f() { return this.dynamicForm.controls; }
get a() { return this.f.attributes as FormArray; }
myfunc(event){
  this.array.push(event.value);
  console.log(this.dynamicForm.controls.attributes);
}

addAttitubute(){

    this.attributeService.getAttributes().subscribe(response =>{
        this.attribute=response;
        this.a.push(this.formBuilder.group({
            attribute: new FormControl (""),
            value: this.formBuilder.array(this.array)
        }));
        this.array=[];
    })     
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...