I'm build a webapp using Angular4 and I'm trying to create a button to call functions on other components.
I was reading other similar questions here, but they have a scenario where the components are child or sibling components with the declaration on the html, e.g.:
<parent-component>
<child-component></child-component>
<sibling-component></sibling-component>
</parent-component>
However, I'm working on a different scenario, because my components are Invoked via routing
and I can't make it work.
Basically I have a header
on the page with some controls to execute functions, but these functions need to trigger/change data on other components loaded via the routing.
For example, I have the routing /store
to show a list of stores, I also have a drawer on this page that I want to show when the user clicks on a button on the header, this is the problem, because I can't propagate the event from the HeaderComponent
to the StoreComponent
.
These are my files:
header.component.ts
@Component({
selector: 'header',
templateUrl: `
<section class="container">
<button (click)="clickFilter()">Open filter</button>
</section>
`
})
export class HeaderComponent {
@Output() onFilter: EventEmitter = new EventEmitter();
clickFilter():void {
this.onFilter.emit('Register click');
}
}
store.component.ts
@Component({
templateUrl: 'store.html'
})
export class StoreComponent {
onFilterClick(event) {
console.log('Fire onFilterClick: ', event);
}
}
// store.html
<article class="card" (onFilter)="onFilterClick($event)">
Test
</article>
But this is not working.
Also, I don't actually need to call a function, I could just pass a boolean value, for example, to bind to a property on the StoreComponent and then toggle the div inside the html file.