使用子组件按钮单击关闭 Kendo Angular Dialog

Posted

技术标签:

【中文标题】使用子组件按钮单击关闭 Kendo Angular Dialog【英文标题】:Close Kendo Angular Dialog using child component button click 【发布时间】:2018-02-11 11:22:15 【问题描述】:

我有一个表单组件,我使用 kendo 对话服务将其嵌入到对话中。我可以调用我的服务并在保存点击时保存我的表单数据。我试图弄清楚如何在点击保存后关闭对话框。我想将所有逻辑保留在对话框组件中,然后从父组件打开对话框。验证和保存调用将发生在对话框组件中。我可以只使用模板并将保存/关闭功能放在父组件中,但我想将其隔离到对话服务使用的子组件中。

ClientComponent.ts

    import  AddClientComponent  from './add-client.component';
import  Component, OnInit  from '@angular/core';
import  ClientService  from '../services/client.service';
import  DialogService, DialogCloseResult, DialogRef  from '@progress/kendo-angular-dialog';

@Component(
    selector: 'clients',
    templateUrl: 'ClientComponent.html',
    styleUrls: ['../app.component.css'],
    moduleId: module.id
)
export class ClientsComponent implements OnInit 
    public clients: any[];
    private title = 'Clients';

    constructor(private clientService: ClientService, private dialogService: DialogService) 

    

    ngOnInit() 
        this.clients = this.clientService.getClients();
    

    public showAddClient() 
        const dialog: DialogRef  = this.dialogService.open(
            title: "Add User",

            // show component
            content: AddClientComponent
        );

        dialog.result.subscribe((result) => 
            if (result instanceof DialogCloseResult) 
                console.log("close");
             else 
                console.log("action", result);
                this.clients = this.clientService.getClients();
            
        );
    

clientComponent.html

<h1>title</h1>

<br/>

<button (click)="showAddClient(dialogActions)" class="k-button">Add Client</button>

<kendo-grid [data]="clients">
    <kendo-grid-column field="Id" title="Id">
    </kendo-grid-column>
    <kendo-grid-column field="FirstName" title="FirstName">
    </kendo-grid-column>
    <kendo-grid-column field="LastName" title="LastName">
    </kendo-grid-column>
    <kendo-grid-column field="DateOfBirth" title="DateOfBirth">
    </kendo-grid-column>
    <kendo-grid-column field="Location" title="Location">
    </kendo-grid-column>
</kendo-grid>

<div kendoDialogContainer></div>

add-client.component.ts

import  Component, Input  from '@angular/core';
import  ClientService  from '../services/client.service';
import  Client  from '../entities/Client';

@Component(
  selector: 'add-client',
  templateUrl: 'AddClient.html',
  moduleId: module.id
)
export class AddClientComponent 

    constructor(private clientService: ClientService) 

    

    public firstName: string;
    public lastName: string;
    public dateOfBirth: Date;
    public address: string;

    public Save() 

        var client = new Client(0, this.firstName, this.lastName, this.dateOfBirth, this.address)
        this.clientService.addClient(client);
    

AddClient.html

<form class="k-form">
    <label class="k-form-field">
        <span>First Name</span>
        <input class="k-textbox" placeholder="Your Name" [(ngModel)]="firstName" name="firstName" />
    </label>
    <label class="k-form-field">
        <span>Last Name</span>
        <input class="k-textbox" placeholder="Your Last Name" [(ngModel)]="lastName" name="lastName" />
    </label>
    <label class="k-form-field">
        <span>Date of Birth</span>
        <kendo-datepicker name="birthDate"
                          [(ngModel)]="birthDate"></kendo-datepicker>
    </label>
    <label class="k-form-field">
        <span>Location</span>
        <input class="k-textbox" placeholder="Perrysburg" [(ngModel)]="location" name="location" />
    </label>

    <button class="k-button pull-right" (click)="Save()" primary="true" style="background-color:deepskyblue">Save</button>
</form>

【问题讨论】:

您可以在 teamviewer 中进行调试吗? 对不起我不是@Aravind 更新add.client.html 文件以发布 @Aravind 添加。 抱歉,请同时更新其他 html 文件 【参考方案1】:

现在有一种更好、更简单的方法可以做到这一点。您可以查看文档

https://www.telerik.com/kendo-angular-ui/components/dialogs/dialog/service/

基本上你要做的就是在子组件中提供一个DialogRef类型的构造函数参数。

add-client.component.ts 看起来像这样:

import  Component, Input  from '@angular/core';
import  ClientService  from '../services/client.service';
import  Client  from '../entities/Client';
import  DialogRef  from '@progress/kendo-angular-dialog';

@Component(
    selector: 'add-client',
    templateUrl: 'AddClient.html',
    moduleId: module.id
) 

export class AddClientComponent 

    constructor(private clientService: ClientService,
    public dialog : DialogRef) 

    

    public firstName: string;
    public lastName: string;
    public dateOfBirth: Date;
    public address: string;

    public Save() 

        var client = new Client(0, this.firstName, this.lastName, this.dateOfBirth, this.address)
        this.clientService.addClient(client);
        this.dialog.close();
    

【讨论】:

【参考方案2】:

您可以在此处查看我对关于防止对话框在按下操作按钮后关闭的类似问题的回答

how to add callbacks to kendo dialog actions

然后您可以在关闭对话框之前直接调用对话框实例上的函数,这样您就可以将所有逻辑保留在对话框组件中。

【讨论】:

以上是关于使用子组件按钮单击关闭 Kendo Angular Dialog的主要内容,如果未能解决你的问题,请参考以下文章

从Angular 5中的子组件更新父布尔值

可以在角度调用父组件功能时单击子组件上的任何按钮? [关闭]

如何让组件在单击按钮时以角度删除自身

在父组件中单击按钮时从子组件执行功能:Angular2

使用关闭按钮关闭 Kendo 窗口

在父组件中显示来自子组件的按钮的结果 - Angular