그래서 검색 필드에 ng2-bootstrap을 사용하고 있습니다. 반환 된 값을 선택하면 선택한 항목의 세부 정보로 아래 창을 새로 고치기를 원합니다. 내 부모 구성 요소에서각도 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>
...
타이프 :
Harry_Ninh의 우수한 제안에서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도 참조하십시오. –