相关疑难解决方法(0)

Angular Observables和Http

我很难将我的大脑缠绕在Angular的观察者身上.我来自PHP的世界,事情肯定不是异步的.

我有一个组件,只显示一般主题的消息列表.现在,我有一个所有消息都属于的主题.如果主题不存在,则应创建该主题.消息和主题调用都是通过REST API完成的.

在非同步世界中,我会按顺序编程.消息服务将查看主题是否存在.如果没有,那么它有主题服务创建它.在有主题后,它会获取该主题中的所有消息.

我知道你订阅了一个observable,但是当需要按顺序发生一系列事情时会发生什么?的角2的HTTP文档经过当一个呼叫由更新英雄列表的一个非常简单的例子.

零件

export class NotificationListComponent implements OnInit {
    constructor(private _notificationService:NotificationService) {
}

***

ngOnInit() {
    this.getNotifications();
}

getNotifications() {
      this._notificationService.getNotifications()
        .subscribe(
            notifications => this.notifications = notifications,
            error => this.errorMessage = <any>error);
}
Run Code Online (Sandbox Code Playgroud)

通知服务

...    
 getNotifications() {
     //  call the topic service here for general topic??

    return this.http.get('/messages?order[datesent]=DESC')
        .map((res) => {
            return res.json()["hydra:member"];
        })
        .map((notifications:Array<any>) => {
            let result:Array<Notification> = [];
            notifications.forEach((jsonNotification) => {
                var Notification:Notification = {
                    message: jsonNotification.message,
                    topic: jsonNotification.topic,
                    datesent: new Date(jsonNotification.datesent), …
Run Code Online (Sandbox Code Playgroud)

observable angular

11
推荐指数
1
解决办法
3642
查看次数

标签 统计

angular ×1

observable ×1