19

Working with Angular2s forms and trying to figure out the process of handling events with selects. I have an object Heros that is stored in the options. What I want to do is that when I hero is selected, trigger an event to the parent component that will do something with the results. However, I can't find a concrete example of being able to receive an event when the selection has changed (ie a new hero in the list as been selected).

interface Hero {
  id: number;
  name: string;
}
@Component({
  selector: 'my-app',
  template:`
  <h1>{{title}}</h1>
  <form>
    <select>
        <option *ngFor="#hero of heros "
                [value]="hero">
            {{hero .name}}
        </option>
    </select>
  </form>
`
})
   export class AppComponent {
   @Input() heros:Observable<Hero>
   @Output("selectedHeroChange") selectedHeroChange:EventEmitter<any> = new EventEmitter

   onHeroChange(hero:Hero){
      this.selectedHeroChange._next(hero);
   }    
}

Thanks in advance!

3
  • Mabe stackoverflow.com/questions/31177984/selects-events-in-angular2 Commented Jan 8, 2016 at 22:05
  • this.selectedHeroChange.emit(hero); Commented Jan 8, 2016 at 22:12
  • @EricMartinez The issues is that it never makes to go the onHeroChange function at all, cause I am unsure of how/when to call it. Commented Jan 8, 2016 at 22:42

3 Answers 3

12

Execute code on select change and use an id property or index as value`:

<select (change)="onHeroChange($event)">
    <option *ngFor="#hero of heros; #i=index"
  [value]="hero.id">
      <!-- [value]="i" -->
        {{hero.name}}
    </option>
</select>

get the selected value from the event

onHeroChange(event:Event):void {
  Hero hero = heros.firstWhere(
      (Hero hero) => hero.id == (event.target as SelectElement).value);
  // Hero hero = heros[int.parse((event.target as SelectElement).value)];
  selectedHero = hero;
  selectedHeroChange.add(hero);
}

See also https://github.com/angular/angular/issues/4843#issuecomment-170147058

See also Binding select element to object in Angular 2

Sign up to request clarification or add additional context in comments.

8 Comments

I was attempting to do this, but what is happening is that the [value]="hero" seems to be converted via toString instead of storing the actual object. To get this solution to work, I would have to have [value]="hero.id" and then I'd need to iterate through my list of for the onchange event and then return.... which seems like a gap (this functionality worked within KO)
For TS your function would look like this: onheroChange(event: Event): void { ... }
Thanks for the hint! Just a little more than a year but feels like ages ago :D
You shouldn't need to implement an output, the <md-tab-group> has that already. You should just need to add an expression that you want to have executed on tab-change where I put ...
Correction to my (deleted) comment above: What about <md-tab-group (selectedIndexChange)="..." github.com/angular/material2/blob/…
|
6

Seem there is a gap in ng2 that select cannot handle objects. Here is a work round for now.

How to use select/option/NgFor on an array of objects in Angular2

Comments

2

You get the object from a select by using

[ngValue]="hero" 

instead of

[value]="hero".

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.