I have a project that uses the angular $ http service to download data from a remote location. I want to use rxjs Observables, so the call in my service is as follows:
userInfo() : Rx.Observable<IUserInfo> {
var url : string = someUrl + this._accessToken;
return Rx.Observable.fromPromise<IUserInfo>( this.$http.get<IUserInfo>( url ) );
}
and my subscriber subscribes as follows:
getUserInfo() : void {
this._googleService.userInfo().subscribe(
( result ) => { this.handleUserInfo( result ) },
( fault : string ) => this.handleError( fault )
)
}
private handleUserInfo( result : IHttpPromiseCallbackArg<IUserInfo> ) : void {
console.log( "User info received at " + new Date() );
this._name = result.data.given_name + " " + result.data.family_name;
this._email = result.data.email;
this._profilePicUrl = result.data.picture;
}
the problem is that although the update of the name, email and pic profile has been updated, these changes are not visible. As soon as something else starts angular $, the changes are applied, but due to the Observable, these changes in the controller occur after the angular digest loop, which is started by calling $ http. This works correctly if my service simply returns a promise to the controller.
? , . , Observables , .