Reputation: 2427
I have a component called home
which as some description and button
inside it. As in below image.
I am reusing the same home
component in another 2 components called car
and bike
.
Here in car
component on clicking the button (i,e button present in the home component), I am calling a component called car-booking
inside a dialog window. This requirement is done now.
But I am re-using home
component in bike
component too. In bike component on clicking the button(i,e button present in the home component) I should call another component (ex bike-booking)
inside the dialog window.
How can I change the (click) function of the particular button on basis of its presence in the component?
The stackblitz DEMO
Upvotes: 0
Views: 1006
Reputation: 5709
You can use @Output
to emit an event to the container, then in the container component you can choose what to do when you receive the event.
HomeComponent
export class HomeComponent {
@Output() openDialog = new EventEmitter();
constructor(public dialog: MatDialog) {}
open(): void {
this.openDialog.emit();
}
}
HomeComponent's template
Hai!! welcome to our application..
<div style="padding:30px;">
<button mat-raised-button (click)="open()" color="primary">ADD</button>
</div>
BikeComponent's template
<app-home (openDialog)="openDialog($event)"></app-home>
BikeComponent ts
export class BikeComponent implements OnInit {
constructor() { }
ngOnInit() {
}
openDialog($event: any) {
// open desired bike component
}
}
CarComponent's template
<app-home (openDialog)="openDialog($event)"></app-home>
CarComponent ts
export class CarComponent implements OnInit {
constructor() { }
ngOnInit() {
}
openDialog($event: any) {
// open desired car component
}
}
Look at official doc: https://angular.io/guide/component-interaction
Upvotes: 1