# Angular Service

The [`DialogService`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogservice) is an [Angular service](https://angular.dev/guide/di/creating-and-using-services#) that provides API calls, which are used to create Dialog instances dynamically.

This means that the service helps creating Dialog instances based on user interactions and without the need to define the components in a template.

## Getting Started

The [Angular dependency injection](https://angular.dev/guide/di%22) injects the [`DialogService`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogservice) automatically in the constructor of the component.

To create new Dialogs:

1. In your application, include an element with the [`kendoDialogContainer`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogcontainerdirective) directive.

   ```html
   <div kendoDialogContainer></div>
   ```

2. To open the Dialog, use the [`open`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogservice#open) method of the service and pass the configuration of the newly created Dialog service as a [`DialogSettings`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogsettings) parameter object. For example, use the [`title`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogsettings#title) property to set the Dialog titlebar text.

   ```ts
   const dialog: DialogRef = this.dialogService.open({
       title: 'Please confirm',
       content: 'Are you sure?',
       actions: [{ text: 'No' }, { text: 'Yes', themeColor: 'primary' }]
   });
   ```

3. To determine the action triggered by the user, subscribe to the [`result`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogref#result) event stream.

   ```ts
   dialog.result.subscribe((result) => {
       if (result instanceof DialogCloseResult) {
           console.log('close');
       } else {
           console.log('action', result);
       }
   });
   ```

> Dialogs created through the [`DialogService`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogservice) automatically close when the user clicks the **Close** title-bar button or any of the action buttons. Depending on the current configuration settings of the service, you can use the available options and prevent this behavior.

The following example demonstrates how to use the Dialog service.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/dialogs/dialog/service/source.txt)

## Rendering the Content Area

The [`DialogService`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogservice) allows you to customize the content area of the created Dialog. This is the area between its title-bar section and the actions positioned under it.

You can specify the contents of the Dialog content area by using:

* [Content Template](https://www.telerik.com/kendo-angular-ui/components/dialogs/dialog/service#content-template)
* [Content Component](https://www.telerik.com/kendo-angular-ui/components/dialogs/dialog/service#content-component)

### Content Template

The Dialog allows the usage of a template expression in its content area. To enable this behavior, pass a [`TemplateRef`](https://angular.dev/api/core/TemplateRef) instance to the [`content`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogsettings#content) property of the [`DialogSettings`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogsettings) configuration object.

The following example demonstrates how to customize the content area with a template.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/dialogs/dialog/service/template/source.txt)

### Content Component

The Dialog allows you to render a component instance in its content area. To enable this behavior, pass the component class name to the [`content`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogsettings#content) property of the [`DialogSettings`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogsettings) configuration object. You could also pass any required input properties to the content component, after the invocation of the [`open`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogservice#open) method of the [`DialogService`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogservice).

The following example demonstrates how to render a component in the content area.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/dialogs/dialog/service/component/source.txt)

### Accessing the Dialog Instance

When using the content component, you may need to access the Dialog instance from it—for example, if you create a custom **Close** button. To achieve this, inject the Dialog instance in the component constructor as a parameter of type [`DialogRef`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogref). The Dialog instance exposes a convenient `close` method.

The following example demonstrates how to access the Dialog instance from the content component.

```ts
import { Component } from '@angular/core';
import { DialogRef } from '@progress/kendo-angular-dialog';

@Component({
    template: `
        <button type="button" (click)="dialog.close()">Close</button>
    `
})
class ChildComponent {
    constructor(
        public dialog: DialogRef
    ) {}
}
```

### Dialog Close Prevention

The Dialog supports two approaches to prevent users from closing it. Choose the one that best fits your scenario:

* [`closable`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogsettings#closable)—Removes the title-bar **Close** button and disables the `Esc` key unconditionally. Use this when dismissal without an explicit choice is never acceptable, for example, when accepting terms and conditions or confirming a destructive action.
* [`preventAction`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogsettings#preventaction)—Keeps the close mechanisms visible but intercepts the close event and applies custom logic to decide whether to allow it. Use this when the Dialog can be dismissed under certain conditions, such as only after a form is valid.

<TabStrip>
<TabStripTab title="closable">

To disable the close mechanisms entirely, set the [`closable`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogsettings#closable) option to `false` in the [`DialogSettings`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogsettings) configuration object:

```ts
const dialog: DialogRef = this.dialogService.open({
    title: 'Terms and Conditions',
    content: 'You must accept the terms before continuing.',
    closable: false,
    actions: [
        { text: 'Decline' },
        { text: 'Accept', themeColor: 'primary' }
    ]
});
```

The following example demonstrates a Dialog opened through the service with `closable` set to `false`.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/dialogs/dialog/service/closable/source.txt)

</TabStripTab>
<TabStripTab title="preventAction">

To prevent closing conditionally, specify the [`preventAction`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogsettings#preventaction) callback as part of the [`DialogSettings`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogsettings) configuration object. This callback allows you to tap into the lifecycle of the Dialog and prevent its closing if needed.

The `preventAction` callback receives a parameter of type [`DialogResult`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogresult), which can be either of the following:

* [`DialogAction`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogaction) if an action was clicked.
* [`DialogCloseResult`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogcloseresult) if the **Close** button of the Dialog was clicked.

The following example demonstrates how to conditionally prevent the closing of the Dialog.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/dialogs/dialog/service/prevent-closing/titlebar-in-settings/source.txt)

</TabStripTab>
</TabStrip>

## Rendering the Actions Area

The [`DialogService`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogservice) allows you to customize the actions area of the created Dialog by utilizing the [`actions`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogsettings#actions) property of the [`DialogSettings`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogsettings) configuration object. To configure the actions, use either of the following approaches:

* [Actions collection](https://www.telerik.com/kendo-angular-ui/components/dialogs/dialog/service#actions-collection)
* [Actions template](https://www.telerik.com/kendo-angular-ui/components/dialogs/dialog/service#actions-template)

### Actions Collection

You can pass the actions as a collection of [`DialogAction`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogaction) objects to the [`actions`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogsettings#actions) property of the [`DialogSettings`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogsettings) configuration object.

```ts
    const dialog: DialogRef = this.dialogService.open({
        ...
        actions: [
            { text: 'No' },
            { text: 'Yes', themeColor: 'primary' }
        ],
    });
```

### Actions Template

The [`DialogService`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogservice) also allows you to show custom actions to the user. To enable this behavior, create the template containing the actions, and pass it as a [`TemplateRef`](https://angular.dev/api/core/TemplateRef) instance to the [`actions`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogsettings#actions) property of the [`DialogSettings`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogsettings) configuration object. When you use this approach, you can configure the buttons in either of the following ways:

* As standalone Kendo UI Buttons.

  ```html
  <kendo-dialog-actions>
      <button kendoButton (click)="onCancelAction()">Cancel</button>
      <button kendoButton (click)="onConfirmAction()" themeColor="primary" [disabled]="!formGroup.valid">
          Submit
      </button>
  </kendo-dialog-actions>
  ```

* As an array of objects that is bound to the [`actions`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogactionscomponent#actions) property of the [`DialogActionsComponent`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogactionscomponent).

  ```html
  <kendo-dialog-actions [actions]="[{text: 'Cancel'}, {text: 'Confirm', themeColor: 'primary'}]">
  </kendo-dialog-actions>
  ```

The following example demonstrates how to create a template for the action buttons.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/dialogs/dialog/service/custom-actions/source.txt)

### Action Prevention

When using the [`actions`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogsettings#actions) property of the [`DialogSettings`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogsettings) configuration object, you can prevent the closing of the Dialog based on the clicked action. To achieve this, utilize the [`preventAction`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogsettings) callback and pass it as part of the dialog settings. This callback allows you to tap into the lifecycle of the Dialog and prevent its closing if needed.

The `preventAction` callback receives a parameter of type [`DialogResult`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogresult), which can be either of the following:

* [`DialogAction`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogaction) if an action was clicked.
* [`DialogCloseResult`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogcloseresult) if the **Close** button of the Dialog was clicked.

The following example demonstrates how to prevent the closing of the Dialog based on the current action.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/dialogs/dialog/service/prevent-closing/actions-in-settings/source.txt)

## Single Component Rendering

The `DialogService` allows you to provide all building blocks of the Dialog (title-bar, content, and actions) as part of a single component in the following way:

1. Create the component and extend the [`DialogContentBase`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogcontentbase) class. This base class is responsible for gluing together the respective title bar, action buttons, and events.

   ```ts
   @Component({ ... })
   export class UserInfoComponent extends DialogContentBase {
       ...
   }
   ```

2. Include the configuration markup for the title-bar, content area, and the actions in the component template.

   ```ts
   @Component({
     selector: 'user-info',
     template: `
       <kendo-dialog-titlebar>
           
       </kendo-dialog-titlebar>

       

       <kendo-dialog-actions>
           
       </kendo-dialog-actions>
   `
   })
   export class UserInfoComponent extends DialogContentBase {
       // Add the component logic and input properties here.
   }
   ```

3. Pass the component class name to the [`content`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogsettings#content) property of the [`DialogSettings`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogsettings) configuration object.

   ```ts
   const dialogRef = this.dialogService.open({
       content: UserInfoComponent,
       // Add other DialogSettings options here if needed.
   });
   ```

4. (Optional) After the invocation of the [`open`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogservice#open) method of the `DialogService`, pass any required input properties to the content component.

   ```ts
   const dialogRef = this.dialogService.open({
       content: UserInfoComponent,
   });

   const userInfo = dialogRef.content.instance;
   userInfo.name = 'admin';
   userInfo.age = 42;
   ```

> When you use the single component rendering approach, you can still configure the title-bar and the actions of the Dialog as part of the [`DialogSettings`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogsettings) object instead. If these are passed as part of the configuration, avoid adding them to the content component markup because this will lead to the rendering of multiple title and action components in the created Dialog.

The following example demonstrates how to implement the suggested approach.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/dialogs/dialog/service/content-component/source.txt)

### TitleBar Close Prevention

To prevent the closing of the Dialog when you use the single component rendering approach, subscribe to the preventable `close` event of the [`DialogTitleBarComponent`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogtitlebarcomponent). This enables you to keep the Dialog opened based on custom logic.

The following example demonstrates how to conditionally prevent the closing of the Dialog.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/dialogs/dialog/service/prevent-closing/titlebar-in-component/source.txt)

## Specifying Custom Dialog Containers

You can also specify the container for the newly created Windows by passing a [`ViewContainerRef`](https://angular.dev/api/core/ViewContainerRef) instance to the [`appendTo`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogsettings) property when you call the [`open`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogservice#open) method of the `DialogService`.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/dialogs/dialog/service/dialog-container/source.txt)

## Specifying CSS Classes and HTML Attributes

The `DialogService` allows you to apply custom CSS Classes and HTML Attributes to the created Dialog by utilizing the [`cssClass`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogsettings#cssclass) and [`htmlAttributes`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogsettings#htmlattributes) options of the [`DialogSettings`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogsettings) configuration object. The following sections demonstrate how to specify:

* [Custom CSS Classes](https://www.telerik.com/kendo-angular-ui/components/dialogs/dialog/service#custom-css-classes)
* [Custom HTML Attributes](https://www.telerik.com/kendo-angular-ui/components/dialogs/dialog/service#custom-html-attributes)

### Custom CSS Classes

To apply custom CSS Classes, specify the [`cssClass`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogsettings#cssclass) option as part of the [`DialogSettings`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogsettings) configuration object. The `cssClass` option supports the union type of values that [NgClass](https://angular.dev/api/common/NgClass) accepts.

> The custom CSS Classes are applied to the wrapper of the Dialog component. If you need to access the actual Dialog element, you can utilize the `.k-dialog` CSS class.

The following example demonstrates how to set custom CSS Classes to the Dialog.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/dialogs/dialog/service/css-class/source.txt)

### Custom HTML Attributes

To apply custom HTML Attributes, specify the [`htmlAttributes`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogsettings#htmlattributes) option as part of the [`DialogSettings`](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogsettings) configuration object.

The `htmlAttributes` option accepts `string key-value pairs` like the following one.

```ts
public showConfirmation(): void {
    this.dialogService.open({
        title: 'Please confirm',
        htmlAttributes: {
            dir: 'rtl'
        }
    });
}
```

The following example demonstrates this approach in action.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/dialogs/dialog/service/html-attributes/source.txt)

## Suggested Links

* [API Reference of the Dialog](https://www.telerik.com/kendo-angular-ui/components/dialogs/api/dialogcomponent)
