Recent Tutorials and Articles
    Dialog Box Using Angular Material Library
    Published on: 25th May 2018
    Posted By: Sumit Sain

    This tutorial helps you implement different type of Dialog Box using Angular Material Library in Angular JS.

    We will be creating following two files in order to demonstrate the implementation of dialog box using Angular Material Library -

    1. index.html file for creating the dialog box.
    2. dialog.js file containing AngularJs controller responsible for initializing the dialog box.

    This is how our project structure looks like -

    Project Structure


    Here is how our HTML file index.html looks like -

    <html lang="en" >
          <link rel="stylesheet" href="">
          <script src=""></script>
          <script src=""></script>
          <script src=""></script>
          <script src=""></script>
          <script src=""></script>
          <script type="text/javascript" src="js/dialog.js"></script>   
       <body ng-app="myApp"> 
          <div id="dialogContainer" ng-controller="Ctrl" layout="row" ng-cloak>
                <h4 style="margin-left:20px;">
                   Alert Box: <md-button style="margin-left:32px;" class="md-primary md-raised" ng-click="showAlert($event)" flex="100"  flex-gt-md="auto">
                <h4 style="margin-left:20px;">
                   Confirm Box: <md-button class="md-primary md-raised" ng-click="showConfirm($event)"  flex="100"  flex-gt-md="auto">
                <h4 style="margin-left:20px;">
                   Custom Box: <md-button class="md-primary md-raised" ng-click="showCustom($event)"  flex="100"  flex-gt-md="auto">
                <div ng-if="status">
                   <b layout="row" layout-align="left left" class="md-padding">

    Let's discuss some of AngularJs specific parts in the above HTML file. Specifically, here are the attributes that need attention -

    1. First we have added some libraries for Angular Material in html file head tag section as given above.
    2. The ng-cloak, use this directive to avoid the undesirable flicker effect caused by the html template display.
    3. <md-content> directive is a container element useful for scrollable content. It achieves this by setting the CSS overflow property to auto so that content can properly scroll.
    4. <md-button> is a button directive with optional ink ripples (default enabled).
    5. Flex: will grow and shrink as needed. Starts with a size of 0%. Same as flex="0".

    Finally, here is the javascript file containing the controller for our dialog box -

    angular.module('myApp', ['ngMaterial']).controller('Ctrl', myAppController);
             function myAppController ($scope, $mdDialog) {
                $scope.status = '';
                var alert;
                $scope.showAlert = showAlertBox;
           	    $scope.items = ["All","Programming","Tutorials"];
                function showAlertBox() {
                   alert = $mdDialog.alert({
                             title: 'Dialog Box',
                             textContent: 'All Programming Tutorials!',
                             ok: 'Close'
                             alert = undefined;
                $scope.showConfirm = function(event) {
                   var confirm = $mdDialog.confirm()
                      .title('Will you subscribe for All Programming Tutorials?')
                      .textContent('Please subscribe now for tutorials.')
                      $ {
                         $scope.status = 'Done!';
                         }, function() {
                            $scope.status = 'Please subscribe.';
                $scope.showCustom = function(event) {
                      clickOutsideToClose: true,
                      scope: $scope,        
                      preserveScope: true,           
                      template: '<md-dialog aria-label="List dialog">' +
                                 '  <md-dialog-content>'+
                                 '    <md-list>'+
                                 '      <md-list-item ng-repeat="item in items">'+
                                 '       <p>* <b>{{item}}</b></p>' +
                                 '      '+
                                 '    </md-list-item></md-list>'+
                                 '  </md-dialog-content>' +
                                 '  <md-dialog-actions>' +
                                 '    <md-button ng-click="closeDialog()" class="md-primary">' +
                                 '      Close' +
                                 '    </md-button>' +
                                 '  </md-dialog-actions>' +
                      controller: function DialogController($scope, $mdDialog) {
                         $scope.closeDialog = function() {

    In the above javascript file, there are a few things to note:

    1. $mdDialog: opens a dialog over the app to inform users about critical information or require them to make decisions.
    2. $mdDialog.alert(); - Builds a preconfigured dialog with the specified message.
    3. $mdDialog.confirm(); - Builds a preconfigured dialog with the specified message. You can call, show and the promise returned will be resolved only if the user clicks the confirm action on the dialog.
    4. $; - Show a dialog with the specified options.
    5. $mdDialog.hide([response]); - Hide an existing dialog and resolve the promise returned from $
    Webpage Output in Browser

    This is how the HTML code above will be displayed in a browser with Dialog Box

    You can see the working demo of the above Multiple Dialog Box here.


    Thank you for reading through the tutorial. In case of any feedback/questions/concerns, you can communicate same to us through your comments and we shall get back to you as soon as possible.

    Posted By: Sumit Sain
    Published on: 25th May 2018

    Comment Form is loading comments...