Angular2组件中访问select事件的变化 [英] Angular2 access a select event change within the component
问题描述
与Angular2s形式的合作,并试图找出处理事件与选择的过程。我有被存储在选择的对象英雄。我想要做的是选择我的英雄时,触发一个事件,将做的结果东西父组件。但是,我找不到能够接收事件时,选择已改变(即一个新的英雄在列表中被选中)一个具体的例子。
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);
}
}
在此先感谢!
推荐答案
似乎没有在这NG2选择不能处理对象的差距。下面是一个圆形的工作现在。
Seem there is a gap in ng2 that select cannot handle objects. Here is a work round for now.
<一个href=\"http://stackoverflow.com/questions/33181936/how-to-use-select-option-ngfor-on-an-array-of-objects-in-angular2\">How对象在Angular2 阵列上使用select /选项/ NgFor
How to use select/option/NgFor on an array of objects in Angular2
这篇关于Angular2组件中访问select事件的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!