The dialog window is a graphical control element or a small window that communicates information between an application and a user. It is very helpful to prompt information, warnings and other messages within the application. For example in the web application when a user tries to access any restricted resource system, it can prompt a warning message in the dialog window.
Ignite UI Angular provides very useful standard and custom dialog windows for the web application.
What we will cover in this article?
- Introduction
- Install Ignite UI for Angular
- Create an Angular Project with Ignite UI
- Import the IgxDialogModule
- Alert Dialog
- Standard Dialog
- Custom Dialog
- Conclusion
Introduction
To show information, warnings, or other messages use the Ignite UI for Angular Dialog Window component or present forms for users to fill out. This component opens a dialog window centered on top of the app content and shows a small window. You can also provide a standard alert message that users can cancel.
Install Ignite UI for Angular
You must have node and npm installed on your machine. If you are new to npm, check out the npm documentation here. https://docs.npmjs.com/cli/install otherwise
Open the command prompt and run.
“npm install -g igniteui-cli”
Create an Angular project with Ignite UI
To create a new project run the below command.
“ng new igniteui-dialog-window-demo”
To get into the project-
“cdigniteui-dialog-window-demo”
Now, it’s time to install the Ignite UI for this project. Run the following command:
“npm install igniteui-angular”
Let’s add the required Ignite UI styles and the “HammerJs” library in the angular-cli.json.
Open the “angular.json” and add below code snippet as shown Listing 1.
- "styles": [
- "../node_modules/igniteui-angular/styles/igniteui-angular.css"
- ]
- "scripts": ["../node_modules/hammerjs/hammer.min.js"]
Listing 1.
Now, we have configured Ignite UI with our Angular project successfully.
Adding Material Icons
-
- @import url('https://fonts.googleapis.com/icon?family=Material+Icons');
Run the following command on command prompt to run the Angular application.
ng serve --open
The application will run using the following address and port “http://localhost:4200”
Import the IgxDialogModule
Open the “app.module.ts” file and import the IgxDatePickerModule in the import section of @NgModule
- import {
- IgxDialogModule
- } from 'igniteui-angular';
- @NgModule({
- imports: [
- IgxDialogModule
- ],
- providers: [],
- bootstrap: [AppComponent]
- })
- export class AppModule {}
Listing 2.
Add the dialog-window-page component
Let’s add the new component named dialog-window-page. To do that run the below command on command prompt.
ng generate component components/dialog-window-page
Listing 3.
Figure 1.
Now, add the <app-dialog-window-page></app-dialog-window-page> in app.component.html file.
Alert Dialog
An alert dialog is only used to show information to the users, we can make any decision using this dialog window, it is just used for showing information. Let’s add the alert dialog. To do that add the igx-dialog directive in “dialog-window-page.component.html” page as shown in listing 4.
- <igx-input-group>
- <igx-prefix>
- <igx-icon>person</igx-icon>
- </igx-prefix>
- <label igxLabel for="name">Name</label>
- <input igxInput id="name" type="text" [(ngModel)]="name" name="name" />
- </igx-input-group>
- <igx-input-group>
- <igx-prefix>
- <igx-icon>home</igx-icon>
- </igx-prefix>
- <label igxLabel>City</label>
- <input igxInput id="city" type="text" [(ngModel)]="city" name="city" />
- </igx-input-group>
- <igx-input-group>
- <igx-prefix>
- <igx-icon>phone</igx-icon>
- </igx-prefix>
- <label igxLabel>Phone</label>
- <input igxInput id="phone" type="number" maxlength="10" [(ngModel)]="phoneNumber" name="phoneNumber" />
- </igx-input-group>
- <button igxButton="raised" igxRipple="white" (click)="alert.open()">Submit</button>
Listing 4.
- <igx-dialog #alert title="Notification" message="Data submitted successfully.!" leftButtonLabel="OK" (onLeftButtonSelect)="alert.close()"></igx-dialog>
Listing 5.
Now run the application using “ng server --open”
Figure 2.
Figure 3.
Standard Dialog
Standard dialog window can allow making a decision on the basis of user input, we can ask the user for “Yes” or “No” (Ok or Cancel).
Let’s add the standard dialog. To do that open the “dialog-window-page.component.html” and add the following HTML code as shown in Listing 6.
Note
We are using two-way binding in this sample to show user inputs on the page as {{message}}.
- <b> {{message}} </b>
- <igx-input-group>
- <igx-prefix>
- <igx-icon>person</igx-icon>
- </igx-prefix>
- <label igxLabel for="name">Name</label>
- <input igxInput id="name" type="text" [(ngModel)]="name" name="name" />
- </igx-input-group>
- <igx-input-group>
- <igx-prefix>
- <igx-icon>home</igx-icon>
- </igx-prefix>
- <label igxLabel>City</label>
- <input igxInput id="city" type="text" [(ngModel)]="city" name="city" />
- </igx-input-group>
- <igx-input-group>
- <igx-prefix>
- <igx-icon>phone</igx-icon>
- </igx-prefix>
- <label igxLabel>Phone</label>
- <input igxInput id="phone" type="number" maxlength="10" [(ngModel)]="phoneNumber" name="phoneNumber" />
- </igx-input-group>
- <button igxButton="raised" igxRipple="white" (click)="dialog.open()">Show Dialog</button>
- <igx-dialog #dialog title="Confirmation" leftButtonLabel="No" (onLeftButtonSelect)="dialog.close()" rightButtonLabel="Yes" (onRightButtonSelect)="onSubmit($event)" message="Are you sure you want to submit the information?">
- </igx-dialog>
Listing 6.
And in dialog-window-page.component.ts add the following code, it contains the “onSubmit” method that executes when the user selects the “Yes” as an option.
- export class DialogWindowPageComponent implements OnInit {
- name: string;
- city: string;
- phoneNumber: string;
- message: string;
- constructor() {}
- ngOnInit() {}
- public onSubmit(event) {
- this.message = 'Your Name:' + this.name + ', City: ' + this.city + ' Phone Number: ' + this.phoneNumber;
- event.dialog.close();
- }
- }
Listing 7.
(onLeftButtonSelect)="dialog.close()" is responsible to close the dialog window. To perform action on “Yes” option add (onRightButtonSelect)="onSubmit($event)" property in HTML and write the onSubmit(event) method in ts file as shown in Listing 6 and 7
Now, run the application to view the output,
Figure 4.
Custom Dialog
Custom dialog window allows you to customize the UI. To add HTML controls in the dialog window, we can add input, label, dropdown, checkbox or etc.
Let’s add the custom dialog window in the project. To do that add the following HTML in “dialog-window-page.component.html” as shown in Listing 7.
- <button igxButton="raised" igRipple="white" (click)="form.open()">Show Dialog</button>
- <igx-dialog #form title="Sign In" leftButtonLabel="Cancel" (onLeftButtonSelect)="form.close()" (onRightButtonSelect)="signIn($event)" rightButtonLabel="Sign In" closeOnOutsideSelect="true">
- <form class="signInForm">
- <igx-input-group>
- <igx-prefix>
- <igx-icon>person</igx-icon>
- </igx-prefix>
- <label igxLabel for="username">Username</label>
- <input igxInput id="username" type="text" [(ngModel)]="userName" name="userName" />
- </igx-input-group>
- <igx-input-group>
- <igx-prefix>
- <igx-icon>lock</igx-icon>
- </igx-prefix>
- <label igxLabel>Password</label>
- <input igxInput id="password" type="password" [(ngModel)]="password" name="password" />
- </igx-input-group>
- </igx-dialog>
Listing 8.
And in dialog-window-page.component.ts add the following code. It contains the “signIn” method that will execute the code when the user will choose the “Sign In” button.
- public signIn(event) {
- this.message = 'Your username:' + this.userName + ' and ' + this.password;
- event.dialog.close();
- }
- export class DialogWindowPageComponent implements OnInit {
- userName: string;
- password: string;
- customDialogInformation: string;
- constructor() {}
- ngOnInit() {}
- public signIn(event) {
- this.customDialogInformation = 'Your username:' + this.userName + ' and ' + this.password;
- event.dialog.close();
- }
- }
Listing 9.
Now, run the application to view the output.
Figure 5.
Figure 6.
Conclusion
The dialog window is a very useful component of Ignite UI to show information to the user. In this article, we saw how to add a dialog window, also see the custom setting for this component. It works responsively and flawlessly on any device of any size.
Learn more, download a free trial, and get started here,