2017-05-26 147 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>