Angular 2:如何从一个组件传递值以在更改检测时刷新另一个组件?

时间:2016-11-16 01:21:15

标签: angular ng2-bootstrap

所以我正在使用ng2-bootstrap typeahead来搜索我所拥有的搜索字段。当我选择返回的值时,我希望它刷新下面的窗格,其中包含来自所选项目的详细信息。

在我的父组件中,我有这个:

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;
}

我如何将从typeahead返回的值传递给处理服务调用的组件,以查找详细信息并将其放在<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
        );
    }
}

1 个答案:

答案 0 :(得分:1)

Harry_Ninh出色的建议,这里是我想出的,允许在不使用@Input和任何组件选择器的情况下进行组件之间的通信:

在我的服务中,我添加了一个主题,允许父母宣布进行了搜索:

@Injectable()
export class ChipFamilyService {
    private searchStringSubject = new Subject<string>();
    private _searchUrl = 'http://localhost:8888/chipfamily/'; 

    constructor(private _http: Http) {
    }

    searchAnnounced$ = this.searchStringSubject.asObservable();

    announceSearch(searchValue: string) {
        this.searchStringSubject.next(searchValue);
    }

在我的父组件中,我刚从我的先行字段中选择了结果后发布了公告:

public onSelectSearchValue(e: TypeaheadMatch): void {
    this.chipFamilyService.announceSearch(e.item.id);
}

在我的子组件中,我订阅了公告,然后根据构造函数中的新数据更新模型:

@Component({
    template: require('./chip-family.component.html')
})
export class ChipFamilyComponent implements OnInit {
    errorMessage: string;
    chipFamilyId: number = 11223;
    chipFamilies: IChipFamily[];
    private gridOptions:GridOptions;
    subscription: Subscription;

    constructor(private chipFamilyService: ChipFamilyService) {
        this.subscription = chipFamilyService.searchAnnounced$.subscribe(
            chipFamilyId => {
                this.chipFamilyId = Number(chipFamilyId);
                this.chipFamilyService.getChipFamily(this.chipFamilyId).subscribe(
                    chipFamilies => this.chipFamilies = chipFamilies,
                    error => this.errorMessage = <any>error
                );
            });

    }

这实现了视图面板基于预先搜索的新数据更新所需的结果。