2017-03-03 76 views
0

我是新的角js 2和我想使用訂閱,但我發現了一些奇怪的行爲..當即時通訊使用html上的對象searchresult它有一些數據,但使用內部的TS文件的數據..有不確定奇怪的行爲在角2訂閱時分配數據

組件的某些問題,訂閱該服務

私人SearchResult所IM:信息搜索結果;

constructor(public searchEngineSvc: SearchEngineService) { 
// this.searchResults = this.searchEngineSvc.retrieveData(); 
this.searchEngineSvc.pushedSearchResult.subscribe(
    data => { 
    this.searchResults = data; 
    console.log(this.searchResults); 
    console.log(this.searchResults.stats); 
    }, 
    error => {console.log(error);} 
); 
    } 

ngOnInit() { 
this.searchResults = this.searchEngineSvc.retrieveData(); 
} 

SERVICE

retrieveData() { 
this.searchResult = {}; 
this.searchResult = new SearchResult(new Stats(), [], []); 
console.log(this.searchResult); 
this.subscription = this.retrieveDataHttpCall().subscribe(
    searchResult => { 
    this.searchResult.data = searchResult.data; 
    this.searchResult.stats = searchResult.stats; 
    var ctr: number = 0; 
    for (var key in searchResult.aggregations) { 
     if (searchResult.aggregations.hasOwnProperty(key)) { 
     this.searchResult.aggregations.push(new Aggregation(key, false, [])); 
     for (var innerkley in searchResult.aggregations[key]) { 
      if (searchResult.aggregations[key].hasOwnProperty(innerkley)) { 
      this.searchResult.aggregations[ctr].aggregationObject.push(new AggregationObjects(innerkley, searchResult.aggregations[key][innerkley], true)); 
      } 
     } 
     ctr++; 
     } 
    }; 
    } 
); 
console.log(this.searchResult); 
this.pushedSearchResult.emit(this.searchResult); 
return this.searchResult; 
} 

這條線從分量將表現出一定的未定義的值

console.log(this.searchResults.stats); 

但這一個示出了它有一些數據

console.log(this.searchResults); 

MODEL

import { Stats } from './stats' 
import { Data } from './data' 
import { Aggregation } from './aggregation' 

export class SearchResult { 
    constructor(
     public stats?:Stats, 
     public data?:Data[], 
     public aggregations?:Aggregation[]){ 
    } 
} 

回答

0

我解決它......把

this.pushedSearchResult.emit(this.searchResult); 

searchResult => {} 

因爲這是我認爲這是一個競爭條件的原因..所以裏面puting它(SearchResult中=> {})會在廣播前確保有數據。

retrieveData() { 
this.searchResult = {}; 
this.searchResult = new SearchResult(new Stats(), [], []); 
this.subscription = this.retrieveDataHttpCall().subscribe(
    searchResult => { 
    this.searchResult.data = searchResult.data; 
    this.searchResult.stats = searchResult.stats; 
    console.log(this.searchResult.stats); 
    var ctr: number = 0; 
    for (var key in searchResult.aggregations) { 
     if (searchResult.aggregations.hasOwnProperty(key)) { 
     this.searchResult.aggregations.push(new Aggregation(key, false, [])); 
     for (var innerkley in searchResult.aggregations[key]) { 
      if (searchResult.aggregations[key].hasOwnProperty(innerkley)) { 
      this.searchResult.aggregations[ctr].aggregationObject.push(new AggregationObjects(innerkley, searchResult.aggregations[key][innerkley], true)); 
       } 
      } 
      ctr++; 
      } 
     }; 
     console.log(this.searchResult); 
     this.pushedSearchResult.emit(this.searchResult); 
     } 
    ); 
    return this.searchResult; 
}