2017-10-12 56 views
0

我有此頁面顯示類別列表,如果用戶點擊某個類別,它將顯示該類別的詳細信息。將從http請求獲取的數據從一個頁面傳遞到另一個頁面

getCategory(id) { 
    const url = 'https://[project-name].firebaseio.com/category/' + id + '.json'; 
    console.log(url); 
    return this.http.get(url) 
    .map(res => { 
     const data = res.json(); 
     console.log(data); 
     return data; 
    }); 
    } 

在類別列表TS:

getCategory(id) { 
    this.firebaseService.getCategory(id).subscribe(
     res => { 
     console.logs(res);//returns back title, description, etc 
     } 
    ); 
    } 

在類別列表HTML:

<a [routerLink]="['/category-details' , tile.id]" mat-button color='primary' (click)="getCategory(tile.id)">View</a> 

在我的類別的細節我想要的數據

我的服務

從類別列表中傳遞給它並顯示數據。下面是ts:

ngOnInit() { 
    this.sub = this.route.params.subscribe(params => { 
     this.id = +params['id']; // (+) converts string 'id' to a number 
    // In a real app: dispatch action to load the details here. 
    }); 
    } 

我是新的HTTP請求,我想了解更多關於此。我究竟做錯了什麼?

回答

0

您可以將數據放入共享服務 - 一個注入到這兩個組件中的服務。

0

在類別列表HTML

<a [routerLink]="['/category-details' , tile.id]" mat-button color='primary' (click)="getCategory(tile.id)">View</a> 

你可以寫

<a [routerLink]="['/category-details' , tile.id]" mat-button color='primary'>View</a> 

既是routerlink和(點擊)函數相互衝突。所以它最好只保留routerLink並擺脫(點擊)功能。

之後,在類別詳細信息頁面中,您可以通過從參數中獲取ID來觸發HTTP請求以獲取有關該類別的數據。

相關問題