i am trying to make a global variable to handle when to show the loader on my website. I followed this but i get the following errors.
Generic Type 'Observer' requires 1 type argument(s) on this line
this.ShowLoaderChange = new Observable((observer:Observer){
also i can't seem to see where they declared the variable for the ChangeObserver as i am using here:
this.ShowLoaderChangeObserver = observer;
and here
this.ShowLoaderChangeObserver.next(this.ShowLoader);
import { Observer } from 'rxjs/Observer';
import { Observable } from 'rxjs/Observable';
import { Injectable } from 'angular2/core';
import { Subject } from 'rxjs/Subject';
@Injectable()
export class LoaderService {
public ShowLoader;
ShowLoaderChange: Observable<any>;
constructor() {
this.ShowLoaderChange = new Observable((observer:Observer){
this.ShowLoaderChangeObserver = observer;
})
}
setData() {
this.ShowLoader = !this.ShowLoader;
this.ShowLoaderChangeObserver.next(this.ShowLoader);
}
}
EDIT: After changing my code as per the answer below and it making sense it get TypeError: Cannot read property 'next' of undefined in [null] this comes from the line
this.ShowLoaderChangeObserver.next(this.ShowLoader);
I am calling the function like this
import {LoaderService} from '../LoaderService'
export class AdminDashboardComponent implements OnInit{
constructor (private loader:LoaderService){}
ngOnInit():any{
this.loader.setData();
} }
EDIT: NEW SERVICE
import { Observer } from 'rxjs/Observer';
import { Observable } from 'rxjs/Observable';
import { Injectable } from 'angular2/core';
import { Subject } from 'rxjs/Subject';
@Injectable()
export class LoaderService {
public ShowLoader: boolean;
ShowLoaderChange: Observable<boolean>;
ShowLoaderChangeObserver: Observer<boolean>;
constructor() {
this.ShowLoaderChange = new Observable((observer:Observer<boolean>)
=> {
this.ShowLoaderChangeObserver = observer;
})
}
setData() {
this.ShowLoader = !this.ShowLoader;
this.ShowLoaderChangeObserver.next(this.ShowLoader);
}
}