Access Angular 6 component library values with 2 w

2019-08-24 12:54发布

Working in Angular 6 I've successfully created a Angular Component Library and added a component that has a drop down control in it.

I've added the neccessary imports in app.module and got my library component to show up!!!

..using its selector

<my-custom-dropdown></my-custom-dropdown> 

The problem I'm having is how do I get the value that is selected from the dropDown in the app.component?

Any help is greatly appreciated!!

1条回答
相关推荐>>
2楼-- · 2019-08-24 13:34

Parent component template:

<my-custom-dropdown (selectedValue)="handleselectedvalue($event)"></my-custom-dropdown>
<!-- Add a handleselectedvalue($event) function in your parent component. $event will contain the selected value -->

In your child component:

@Output() selectedValue = new EventEmitter</*type of selected value goes here*/>();

handleSelection(event) {
    this.selectedValue.emit(event);
}

Child component template:

<!-- Child component template -->
<someElement (click)="handleSelection($event)"></someElement>
查看更多
登录 后发表回答