繁体   English   中英

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

[英]Angular 4: Output data from dynamic modal to component

我正在使用材质设计,并设置了一个dialogService用于动态加载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();
    }
}

./modules/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>

./modules/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事件定义一个输出事件,并将结果作为事件参数传递。其他组件可以订阅该事件以处理结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM