0

I want to change all of my EventEmitter to Subjects since it is the recommended way to share variables and states instead of emitting an event.

But now I've got a problem and I just don't understand how to solve it. Here is the sample code:

@Injectable()
export class TicketsService implements OnInit {
    private _url = "http://127.0.0.1:3000/api/getAllTickets";
    private _refreshTickets:Subject<JSON> = new Subject<JSON>();
    public allTickets:JSON;

    private _url_postNewTicket = "http://127.0.0.1:3000/api/postNewTicket";    

    constructor(private _http:AuthHttp) {
        this.allTickets = this._refreshTickets
            .mergeMap(() => this._http.get(this._url))
            .map((res:Response) => res.json())
            .cache();

    }

    refreshAllTickets() {
        console.log("refresh all tickets..");
        this._refreshTickets.next(null);
    }
}

The compiler gives me the following error:

public/angular/app/tickets.service.ts(26,9): error TS2322: Type 'Observable' is not assignable to type 'JSON'. [1] Property 'parse' is missing in type 'Observable'.

I guess I could write allTickets:JSON as allTickets:any but in the component I use ngFor to iterate - so I get the error from ngFor that it needs an array.

Thanks in advance!

danday74
  • 52,471
  • 49
  • 232
  • 283
malachi54
  • 67
  • 1
  • 1
  • 5

1 Answers1

1

You can change allTickets to be of type: Observable<any> and then in your ngFor binding you can use the async pipe like so:

*ngFor="let ticket of allTickets | async"
Marc Harry
  • 2,410
  • 19
  • 21