角4:从动态模态到组件的输出数据



我正在使用材料设计,并设置了用于动态加载mddialog的对话。我正在尝试使用搜索过滤器进行搜索对话框,该对话将其提交时,它会将您带到搜索组件路由。而且我无法弄清楚如何将搜索数据输出到搜索量组件中。

./dialog-service.ts

@Injectable()
    export class DialogService {
    private dynamicModalComponent: DialogComponent;
    public init(dynModal: DialogComponent) {
        this.dynamicModalComponent = dynModal;
    }
    public show(component: Type<any>, configuration?: MdDialogConfig) {
        this.dynamicModalComponent.showModal(component, configuration);
    }
    public hide() {
        this.dynamicModalComponent.hideModal();
    }
}

./模块/search.component.html

<div class="search-component">
<h2>Search</h2>
<md-input-container class="full-width search">
    <input mdInput placeholder="search" color="primary" />
</md-input-container>
<div class="radio-groups">
    <md-radio-group class="radio-buttons" [(ngModel)]="searchFilterValue">
        <md-radio-button class="r-button" [value]="sf.value" *ngFor="let sf 
of searchFilter">
            {{sf.name}}
        </md-radio-button>
    </md-radio-group>
</div>
<md-dialog-actions class="actions">
    <button md-button (click)="hide()">Cancel</button>
    <button md-raised-button (click)="search()" 
          color="primary">Search</button>
</md-dialog-actions>
</div>

./模块/search.component.ts

import {Component, OnInit} from "@angular/core";
import {DialogService} from "../dialog/dialog.service";
import {Router} from "@angular/router";
@Component({
    selector: 'search',
    templateUrl: './search.component.html',
    styleUrls:['./search.component.scss']
})
export class SearchComponent implements OnInit {
searchFilterValue;
searchFilter = [
    {
        name: 'Groups',
        value: 'groups',
    },
    {
        name: 'People',
        value: 'users',
    },
    {
        name: 'Events',
        value: 'events',
    },
    {
        name: 'Posts',
        value: 'posts',
    }
];
constructor(private _dialogService: DialogService,
            private router: Router){
    this.searchFilterValue = 'groups';
}
ngOnInit(){}
hide() {
    this._dialogService.hide();
}
search() {
    this.hide();
    this.router.navigate(['/search']);
}
}

您有几个选择:

1)您可以使用可选或查询路由参数。然后,作为router.navigate的一部分,您还将传递参数。如果您需要将数据直接传递到另一个组件,这是一个很好的选择。

2)另一个选择是构建服务。该服务保留在搜索过滤器值上。搜索组件将值设置为服务,然后组件从服务中读取值。

这些选项中的一种听起来好像可以为您工作吗?

您可以为hide/close事件定义输出事件,并将结果通过事件参数。其他组件可以订阅此类事件以处理结果。

最新更新