passing one component data to other component

这一生的挚爱 提交于 2019-12-06 16:17:45

You can define a factory service (shared data service) and have a array of objects there then fill array from your response and use this array in other component by including the service tag on component.

you can refer this link

This is a asynchrounous function:

getSearchbyDistrictCategoryCity(searchObj) {
// console.log(searchObj);
// let body = JSON.stringify({ searchObj });
let headers = new Headers();
headers.append('Content-Type', 'application/json');

return this.http.post(this.searchAllUrl, searchObj, { headers: headers })
  .map((response: Response) => response.json())
  .catch(this.handleError);
}

Here you are assigning the result in the constructor:

export class SearchdetailsComponent {

  searchedResults:any;

  constructor(private searchService: searchService) {
    this.searchedResults = this.searchService.searchResults;
    console.log(this.searchedResults);  
  }
}

At the moment you are assigning the searchService.searchResults to the search results in your SearchdetailsComponent the function in your search service may not be complete.

You could access the searchService.searchResults from the template of your search details component because it's public. A better way would be to subscribe to the search function from your service in your search details too.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!