How to “walk” inside two arrays using the same *ngFor?

后端 未结 3 1482
北荒
北荒 2020-12-22 07:29

Basically I have two arrays, and I would like to store their values at the same time using a *ngFor, I did something like this just to show what I would expect:

3条回答
  •  借酒劲吻你
    2020-12-22 08:13

    I believe your mistake is having 2 arrays of controls instead of one array of groups:

    alternativeFieldsAndValues: this.fbuilder.array([]),
    

    and then:

     get alternativeFieldsAndValues() {
       return this.form.get('alternativeFieldsAndValues') as FormArray;
     }
    
      addAlternativeFieldAndValue() {
        this.alternativeFieldsAndValues.push(this.fbuilder.group({
          titulo: '',
          conteudo: ''
        }));
      }
    
      removeAlternativeFieldAndValue(index: any) {
        this.alternativeFieldsAndValues.removeAt(index);
      }
    

    make sure to refactor all names properly then do this:

     
    Titulo Conteúdo

    that being said, you could accomplish exactly what you asked like this:

       
    Titulo Conteúdo

    but this does require the two lists are ALWAYS of the same length.

提交回复
热议问题