分配数据时角度2订阅的奇怪行为

时间:2017-03-03 02:57:45

标签: angular angular2-services

我是角度js 2的新手,我正在尝试使用订阅但我发现了一些奇怪的行为..当我在html上使用对象searchresult它有一些数据但在ts文件内部使用数据..我有一些未定义的问题

订阅服务的COMPONENT

私人搜索结果:SearchResult;

  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[]){
    }
}

1 个答案:

答案 0 :(得分:0)

我解决了......放

this.pushedSearchResult.emit(this.searchResult);

searchResult => {}

原因是我认为存在竞争条件..因此将其置于(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;
}