2017-05-26 66 views
0

我正在開發一個ANgular 2應用程序。Angular 2 - 調用組件

我有一個小的窗體,其中home.component.ts,它看起來像下面。 enter image description here

一旦用戶點擊提交按鈕。數據表應該加載匹配的結果。如下所示。這是一個不同的組件result.component.ts

enter image description here

什麼叫從home.component的result.component最好的方法? 第一個組件應該將結果數據集傳遞給第二個組件。

+2

https://angular.io/docs/ts/latest/cookbook/component-communication.html#!#parent-給我孩子 – echonax

+0

我會經歷這個。謝謝@echonax –

+0

與'home.component'相同的組件中的數據表? Datatable如何加載新結果?所有的數據都被加載並過濾了嗎?或者它會調用一個到服務器的'http'調用並獲取新的結果? – CozyAzure

回答

0

您可以顯示其選擇器在@Component指令定義的結果,並通過@input結果傳遞:

result.component.ts

@Component({ 
    selector: 'search-result', 
    teplateUrl: './result.component.html' 
}) 
export class ResultComponent { 
    @Input() result: Result; 
} 

家。 component.ts

export class HomeComponent { 
    searchString: string; 
    result: Result; 

    performSearch(searchString: string) { 
    // perform API call here and save result to this.result 
    } 
} 

home.componen t.html

<label for="q"><strong>Territory:</strong></label><br> 
<input type="text" name="q" ([ngModel])="searchString"><br> 
<button (click)="performSearch(searchString)">Search</button> 
Result: 
<search-result result="result"></search-result> 

result.component.html

<table> 
    <tr *ngFor="let item of result"> 
    <td>{{ item }} 
    </tr> 
</table>