javascript - Angular 4 : Output data from dynamic modal to component

标签 javascript angular typescript material-design

我正在使用 Material Design,并设置了一个用于动态加载 MdDialog 的 dialogService。我正在尝试制作一个带有搜索过滤器的搜索对话框,当提交时它会将您带到搜索结果组件路径。而且我不知道如何将搜索数据输出到搜索结果组件。

./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) 另一种选择是构建服务。该服务保留搜索过滤器值。搜索组件将值设置到服务中,然后该组件从服务中读取值。

这些选项中的一个听起来是否适合您?

关于javascript - Angular 4 : Output data from dynamic modal to component,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46085373/

相关文章:

javascript - ng-deep 在 Angular 内部是如何工作的

angular - 如何在 Angular 4 中插值时插入模板?

javascript - 无法全局劫持/替换 Node 的要求

javascript - 在jQuery中使用toggleClass缩小div

angularjs - 在 Angular 2 中,当我尝试使用双向绑定(bind)时 ngIF 不起作用

javascript - 使用 Object.entries 时保留类型

typescript - NestJs如何测试响应 Controller

javascript - 在 Angular 2 中从数组加载项目

javascript - odata 和 CamelCase

javascript - 如何用户异步守护?