0
因此,我使用ng2-bootstrap typeahead作爲我擁有的搜索字段。當我選擇返回的值時,我希望它使用來自所選項目的詳細信息來刷新下面的窗格。Angular 2:如何從一個組件傳遞值以刷新更改檢測的另一個組件?
在我父組件,我有這樣的:
HTML:
<input [(ngModel)]="asyncSelected"
[typeahead]="dataSource"
(typeaheadLoading)="changeTypeaheadLoading($event)"
(typeaheadNoResults)="changeTypeaheadNoResults($event)"
(typeaheadOnSelect)="onSelectSearchValue($event)"
[typeaheadOptionsLimit]="7"
[typeaheadMinLength]="3"
[typeaheadWaitMs]="500"
[typeaheadOptionField]="'name'"
name="searchField"
class="form-control" style="width: 250px;" placeholder="Search Chip Families">
...
<div class="container-fluid">
<router-outlet></router-outlet>
</div>
打字稿:
public onSelectSearchValue(e: TypeaheadMatch): void {
this.chipFamilyId = e.item.id;
}
它怎麼說,我通過這個值回來從預輸入到我的組件處理對服務的調用以查找細節並將其放入<router-outlet>
?使用更改檢測似乎不起作用。
我的孩子組成如下:
HTML:
<div class="content" *ngIf='chipFamilies && chipFamilies.length'>
<ol class="breadcrumb">
<li><a [routerLink]="['/home']">Home</a></li>
<li><a [routerLink]="['/chipFamily']">{{chipFamilies.Hierarchy}}</a></li>
<li class="active">{{chipFamilies.Name}}</li>
</ol>
<h2>{{chipFamilies.Hierarchy}}</h2>
...
打字稿:
export class ChipFamilyComponent implements OnInit, OnChanges {
errorMessage: string;
@Input() chipFamilyId: number = 11223;
chipFamilies: IChipFamily[];
constructor(private _adminService: ChipFamilyService) {
}
ngOnInit(): void {
//Initially load 11223 on load
this._adminService.getChipFamily(this.chipFamilyId).subscribe(
chipFamilies => this.chipFamilies = chipFamilies,
error => this.errorMessage = <any>error
);
}
ngOnChanges(changes:{[propKey: string]: SimpleChange}) {
debugger;
this._adminService.getChipFamily(this.chipFamilyId).subscribe(
chipFamilies => this.chipFamilies = chipFamilies,
error => this.errorMessage = <any>error
);
}
}
@Input()用於直接父子組件之間的通信。就你而言,你的中間有'',所以標籤不再相關。相反,您應該使用通用服務和「Subject」來在單獨的組件中觸發更改事件。 –
參見https://angular.io/docs/ts/latest/cookbook/component-communication.html#!#bidirectional-service –