与lodash库的Angular2异步问题?

时间:2017-09-12 21:41:00

标签: angular

我有一个订阅行为主题的组件。我的第二个组件订阅了另一个行为主题。它希望接收我们从其他组件返回的数据。

在我的第一次订阅中,一旦我获得了数据,我就会使用lodash检查其中是否存在具有特定key/value的对象。

我所期待的只是true/false bool。这就是我发送到我的组件#2正在监听的其他订阅的内容。

当尝试将lodash函数的结果发送到我的其他订阅时,它永远不会得到它。这里有某种类型的异步问题吗? _find是否有我需要使用的回调?

// Component 1 - Subscribed to a behavior subject. Upon receiving data, calls next on another behavior subject to send it some of the data we received

this._mapsService.uiMapVersionsData
    .subscribe(
        results => {
            if (results) {
                this.mapVersionsData = results;

                // Is there an async issue going on here with passing data to the lodash libary and getting its value back?
                this._mapsService.updateIsDraftData((_.find(results, { IsDraft: '1' }) ? true : false)); // Subscriber never gets anything

                // Other Tests
                this._mapsService.updateIsDraftData(false); // Subscriber receives false
                console.log((_.find(results, { IsDraft: '1' }) ? true : false)) // Prints false
            }
        }
    );


// Component 2 subscribed to another behavior subject trying to receive the data from component 1
this._mapsService.uiIsDraftData
    .subscribe(
        results => {
            if (results) {
                this.isDraft = results;
                console.log('I am a subscriber ' + this.isDraft)
            }
        }
    );


// Service
private uiIsDraftSub = new BehaviorSubject<boolean>(null);
uiIsDraftData = this.uiIsDraftSub.asObservable();

....

updateIsDraftData(isDraft) {
    this.uiIsDraftSub.next(isDraft);
}

0 个答案:

没有答案