How to return all elements of the Angular array

Asked

Viewed 983 times

0

why is returning only the last position of the array instead of all ???

showChildModal3(Candidato: Candidato) {

this.CandidatoService.getCandidatoById(+Candidato.id)
.subscribe( data => {

  for (let c of Candidato.veiculos) {
  this.selectedObjects = []
  this.selectedObjects.push([c.id]);
  this.formularioEdit.patchValue(
    {
     nome:Candidato.nome,
     email: Candidato.email,
     cpf: Candidato.cpf,
     rua: Candidato.rua,
     bairro: Candidato.bairro,
     cidade: Candidato.cidade,
     UF: Candidato.UF,
     tipo: Candidato.tipo_cliente,
     veiculos: this.selectedObjects
    });
  }
});

 this.lgModal3.show();

      }
  • I read in the comments that you are using Primeng. In case you want to list the items in a table?

  • i have an array of obejtos coming from an api

  • From what I understand of your code, you are doing: 1- Receiving an object Candidato by parameter. 2 - Searching for a Candidato for Service(you have already received the object by parameter). 3- You are making a for of Candidato.veiculo (did not understand this part). 4- Always at the beginning of for you empty the selectedObjects and inserts a id inside (that way, there will always be only one id in the array). 5- No patchValue you are taking the values that come from the object that is in the parameter, and not from the data returning from service.

2 answers

0

Man I was in the same trouble and this here worked for me. Search for "Select with custom Trigger text" on the page that has an example of how to do according to the documentation.

here my code that worked, ts ta very similar to the example ai link <mat-form-field> <mat-label>Carros:</mat-label> <mat-select id="slt_carro" [formControl]="toppings" [(ngModel)]="usuario.carros" [(value)]="usuario.carros" multiple> <mat-option *ngFor="let carro of carros" [value]="carro.id" >{{carro.nome}}</mat-option> </mat-select> </mat-form-field>

I hope it helps you!

  • you already used the primeng mano?

  • mano I did exactly as it is there, but I wasn’t returning the ones that were selected, so I tried anyway and this my Cód ai worked, it may be that the first works out tbm, I’m studying angular a little...

0


Hi,

Assuming your code is correct (which I’m in doubt), you could make the following change, maybe it works:

showChildModal3(Candidato: Candidato) { this.CandidatoService.getCandidatoById(+Candidato.id)
.subscribe( data => {


    this.selectedObjects = []
    const valores = [];

    for (let c of Candidato.veiculos) {
      this.selectedObjects.push([c.id]);
        valores.push(
            {
                 nome:c.nome,
                 email: c.email,
                 cpf: c.cpf,
                 rua: c.rua,
                 bairro: c.bairro,
                 cidade: c.cidade,
                 UF: c.UF,
                 tipo: c.tipo_cliente,
                 veiculos: this.selectedObjects //??
            }
        )
    }

    this.formularioEdit.patchValue(valores);
});
this.lgModal3.show();}

Furthermore, you could change this for by an Array.map(fn)(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)

You could see in more detail about reactive Forms: https://angular.io/guide/reactive-forms

Good luck.

  • 1

    Brother fight worked Aki !!!

  • @Grood does not forget to accept his answer as correct ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.