简体   繁体   English

Angular2异步数据问题

[英]Angular2 Async Data Issue

I have a component that is reliant upon an asynchronously fetched object. 我有一个依赖于异步获取对象的组件。 There are also child components in the template that rely on some data from this same object. 模板中还有一些子组件依赖于同一对象中的某些数据。

The problem I am having seems to be a classic race condition, but I'm not familiar enough with Angular 2 to understand the solution. 我遇到的问题似乎是一个典型的竞争条件,但我对Angular 2不太熟悉,无法理解解决方案。

Take this for instance: 以此为例:

export class SampleComponent {
    constructor(service: SomeService) {
        this.service = service;
        this._loadData();
    }

    private _loadData() {
        this.service.getData().subscribe(data => this.data = data);
    }
}

But in the template, I have child components to display certain parts of this.data : 但是在模板中,我有子组件来显示this.data某些部分:

<taglist tags="data?.tags"></taglist>

Now the Component for taglist looks something like: 现在taglist的组件看起来像:

@Component({
    selector: 'taglist',
    directives: [NgFor],
    inputs: ['tags'],
    template: `<span *ngFor="#tag of tags">{{ tag }}</span>`
})

export class TagList {
    public tags: Array<string> = [];

    constructor() {
        //
    }
}

Because tags input is received from an async loaded dataset, its not present when the Tag Component is initialized. 由于标记输入是从异步加载的数据集接收的,因此在标记组件初始化时不存在。 What can I do so that when this.data load is finished, the sub components that use it will automatically access the newly loaded data? 我该怎么办,当这个this.data加载完成后,使用它的子组件会自动访问新加载的数据?

Thank you for any insight you may be able to provide me! 感谢您提供给我的任何见解!

Implement ngOnChanges() ( https://angular.io/docs/ts/latest/api/core/OnChanges-interface.html ) 实现ngOnChanges()https://angular.io/docs/ts/latest/api/core/OnChanges-interface.html

@Component({
    selector: 'taglist',
    inputs: ['tags'],
    template: `<span *ngFor="let tag of tags">{{ tag }}</span>`
})

export class TagList {
    public tags: Array<string> = [];

    constructor() {
        //
    }

    ngOnChanges(changes: {[propName: string]: SimpleChange}) {
      console.log('ngOnChanges - tags = ' + changes['tags'].currentValue);
    }
}

For angular to handle the binding also use 对于角度处理绑定也使用

<taglist [tags]="data?.tags"></taglist> or <taglist tags="{{data?.tags}}"></taglist>

otherwise it's a simple string to attribute assignment Angular doesn't handle. 否则它是一个简单的字符串属性赋值Angular不处理。

This is the usual use case for the built-in async pipe, that is meant to be used to easily consume observables. 这是内置async管道的常用用例,用于轻松使用observable。

Try the following, first create a getter than returns directly the service layer observable: 尝试以下方法,首先创建一个getter而不是直接返回服务层observable:

get data(): Observable {
    return this.service.getData();
}

Then you can consume this observable directly in the template using the async pipe: 然后,您可以使用async管道直接在模板中使用此observable:

<span *ngFor="let tag of data | async">{{ tag }}</span>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM