A common scenario is to have a set of tabs at the bottom of a view and then to indicate the number of “whatever” for a particular tab inside a badge.
With ionic 2, if the tabs are in one component/page, and the page with the functionality which impacts the number of “whatever” items for the badge are in a different page, what is the proper way to have communication from the latter page to the tabs page, so that the number of items in the tabs badge dynamically updates?
yeah but i do not recommend using the ionic events. The team around angular 2 had their reasons to not implement such things ;).
The want to garantuee a easy reproducable dataflow --> so you would use subjects/observables and services.
With events you can trigger any event somewhere in your app. And somehow, somewhere anotherpart is listen to it.
...
private listSizeSubject: Subject<number>;
private _listSize: Observable<number>;
...
constructor(){
...
this.listSizeSubject = new Subject();
this._listSize = this.listSizeSubject.asObservable();
}
get listSize(){
return this._listSize;
}
public addItem(product: ProductInterface){
this.products.push(product);
this.listSizeSubject.next(this.products.length);//next method updates the stream value
}
...
Basically you use a Service to be used by the different tabs, so you keep your Observable there, and we need to update the sequence stream so we use a Subject object which is an Observable and an Observer.
Note that we are using async pipe so it subscribes to the observable that we passed, the pipe that gets the value out of it.
Hope that helps everyone, it took me awhile, and most of people are using localStorage or Events.