how to manipulate an object inside an array in typescript

Asked

Viewed 345 times

0

I use the last version of the angular, and I’m having a hard time passing a value to the field, follow details below

I am pulling data from an api and sending in list to a select

<option *ngFor="let perfil of perfilAdd">{{perfil.name}}</option>

done this, by selecting I need him to return to me name and id, unfortunately the object id is null, so I need it to be manipulated, so when selecting it returns the right value..

Can you help me?

  • what is null is the "profile.id" item or the <option> tag? If it is the tag, you can complement it with: <option [value]="profile.id">{profile.name}</option>

1 answer

1


If you only want the value of the selected field, you don’t even need to fill in the attribute value:

<select (change)="onChange($event.target.value)">
    <option *ngFor="let perfil of perfilAdd" >{{perfil.name}}</option>
</select>

onChange(deviceValue) {
    console.log(deviceValue);
}

If you want to manipulate, check the element value the profile id.

<select [ngModel]="perfilSelecionado" (ngModelChange)="onChange($event)" name="sel2">
    <option [value]="perfil.id" *ngFor="let perfil of perfilAdd">{{perfil.name}}</option>
</select>

export class AppComponent  {
  name = 'Angular';
  perfilAdd = [{id:1, name:"nome1"},{id:2, name:"nome2"}];

onChange(event) {
  console.log (event);
}

}

More details on that reply.

The code working here.

Browser other questions tagged

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