Angular2 可观察回调

标签 angular observable

我试图在可观察对象完成后实现一个简单的回调,这是我的代码:

ngOnInit() {
    this.propertyService
    .getProperties()
    .subscribe(
        (data: ContentPropertyModel[]) => this.properties = data
    );
    this.status = 'active';
}

然而,状态在可观察对象运行之前发生了变化。关于在 observable 运行后如何更改 this.status 的任何想法?

最佳答案

this.status 移动到您的订阅处理程序:

ngOnInit() {
    this.propertyService
    .getProperties()
    .subscribe(
        (data: ContentPropertyModel[]) => {
            this.properties = data
            this.status = 'active';
        }
    );
}

关于Angular2 可观察回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42265182/

相关文章:

typescript - `share()` 和 `publish().refCount()` 之间的区别

node.js - 将项目发布到 JSON 服务器数据库 - Angular

angular - 在子模块中使用导入模块中的组件

angular - Ionic 4 - 从 Popover 传回数据而不关闭

java - Observer - 围绕电子邮件接收逻辑的可观察模型

javascript - 在 Angular 应用程序中对 Observable 的 POST 请求使用 switchMap

javascript - 在 Angular 2 应用程序中使用 execCommand

html - Angular 2 在条件下选择了选项

观察者和 getstyleclass() 监听器之间的 Javafx 冲突

forms - 初始绑定(bind)时的 Angular2 ControlGroup valueChanges