简体   繁体   English

订阅第一次没有工作的主题

[英]subscribing to a subject not working for first time oninit

i have a service which get some data from server and the updates some subject and in my component's ngOnInit function i am subscribing to that subject. 我有一个服务从服务器获取一些数据和更新一些主题和我的组件的ngOnInit功能我订阅该主题。 I have confirmed subject is being updated correctly. 我已确认主题正在正确更新。

following is my service's function which gets data from server and updated the subject. 以下是我的服务功能,它从服务器获取数据并更新主题。

getInboxData(id?) {
    this.inboxID = id;
    this.loadingData.next( // tells to start showing loading animation
    {
      showloading: true,
      clearDocuments: this.shouldClearDocuments()
    });

    this.getInboxCount();
    this.dashboardService.getInbox(id, this.page)
      .subscribe((response) => {
        for (const inbox of response['results']) {
          this.documents.push(inbox.document);
          // tells to stop showing loading animation
          this.loadingData.next({showloading: false, clearDocuments: this.shouldClearDocuments()});
        }
        if (response.next != null) {
          this.page++;
          // this.getInboxData(id);
        }else {
          this.page = 1; // reset
        }
        this.documentsData.next(response);

      });
  }

i am subscribing to documentsData in my component. 我订阅了我的组件中的documentsData following is component's code. 以下是组件的代码。

ngOnInit() {
    this.dashboardDataService.getInboxData(); 
    this.dashboardDataService.documentsData
      .subscribe((response) => {
        this.isLoadingMoreResult = false;
        this.loading = false;

        for (const entry of Object.values(response.results)){ // save new entries
          this.documents.push(entry);
        }

        this.documentsLoaded = Promise.resolve(true);
        this.filteredDocuments = this.documents; // both should be same when user gets new data
        console.log(this.filteredDocuments);
        $('#documentContentSearch').val(''); // reset text in searchBar

        // if (this.documents.length >= 8) { // if list is more than 8 then show scroll
        $('#tableContainer').css({'height': $('#mySideBar').innerHeight() - 195});
        const $myThis = this;

        $('#tableContainer').scroll(function(){
          if (!$myThis.isLoadingMoreResult) { // if it is not already loading result
            $myThis.loadMoreResults();
          }
        });

        $('#dropdown').scroll(function(){
          if (!$myThis.isLoadingMoreResult) { // if it is not already loading result
            $myThis.loadMoreResults();
          }
        });

        // load two results in first time
        if (this.dashboardDataService.page === 2) {
          this.loadMoreResults();
        }
    });
  }

but it is not getting response from suject when i navigate to this component from another component. 但是当我从另一个组件导航到这个组件时,它没有从suject获得响应。 But upon renavigating to same component from this component it is getting response correctly. 但是在从该组件重新导航到相同的组件时,它正在得到正确的响应。 can somebody help me understanding what is goin on here? 有人可以帮我理解这里的东西吗?

You subscribe to the subject after requesting the data. 您在请求数据后订阅了主题。

You have to subscribe and then request the data. 您必须订阅然后请求数据。

ngOnInit() {
    this.dashboardDataService.documentsData.subscribe(...);
    this.dashboardDataService.getInboxData(); 
}

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

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