如何通過方法getUserInfo()更改服務中的Observable中的值?Angular Observable
@Injectable()
export class UserInfoService {
public User: UserInfo = {
firstName: "O",
lastName: "K",
email: "[email protected]",
country: "uk",
avatarUrl: null,
receiveNotifications: true
}
private Obser: Observable<UserInfo> = new Observable(observer => {
observer.next(this.User);
});
getUserInfo(): Observable<UserInfo> {
return this.Obser;
}
}
使用服務:
openDialog() {
let subscription = this.UserService.getUserInfo().subscribe(data =>(data=this.data));
//What should I write here to change the value inside the observable? Please write if it is not difficult
}
我試圖解釋一切詳細。如果您有任何問題,歡迎您。 –
你應該在Subject上使用'asObservable()'來爲它返回一個observable。 –
你的意思是? '返回this.userData.toObservable()'?我可以在哪裏讀到它?我發現只有[this](http://xgrommx.github.io/rx-book/content/notification/notification_instance_methods/toobservable.html) –