Angular material for transferring data to a predefined dialog

I am using Angular Material, and I created a simple settings dialog using $ mdDialogProvide:

angular.module('starterApp').config([
  '$mdDialogProvider',
  function ($mdDialogProvider) {
    $mdDialogProvider.addPreset('warning', {
      options: function () {
        return {
          template:
          '<md-dialog>' +
          '{{dialog.warning}}' +
          '</md-dialog>',
          controllerAs: 'dialog',
          theme: 'warning'
        };
      }
    });
  }
]);

And I want to send a warning message about his call. I tried to pass a message, for example, as follows:

    $mdDialog.show(
      $mdDialog.warning({
        locals: {
          warning: 'Warning message'
        }
      })
    );

But that does not work.

In fact, I checked a lot of solutions, but none of them work. The documentation also does not have such an example.

Is it possible to pass some date to a predefined dialog?

+4
source share
2 answers

Here is one way to do this - CodePen

Markup

<div ng-controller="AppCtrl" ng-cloak="" ng-app="MyApp">
  <md-button ng-click="showDialog()">Show Dialog</md-button>
</div>

Js

angular.module('MyApp',['ngMaterial', 'ngMessages'])

.config([
  '$mdDialogProvider',
  function ($mdDialogProvider) {
    $mdDialogProvider.addPreset('warning', {
      options: function () {
        return {
          template:
          '<md-dialog aria-label="Dialog">' +
          '{{warning}}' +
          '</md-dialog>',
          controller: DialogController,
          theme: 'warning',
          clickOutsideToClose: true
        };
      }
    });

    function DialogController($scope, $mdDialog, locals) {
      console.log(locals);
      $scope.warning = locals.warning;
    }
  }
])

.controller('AppCtrl', function($scope, $mdDialog) {
  $scope.showDialog = function () {
    $mdDialog.show(
      $mdDialog.warning({
        locals: {
          warning: 'Warning message'
        }
      })
    );
  }
});
+5
source

Fast way ES6

let warning = 'Warning message';

$mdDialog.show({
    templateUrl: 'dialog.template.html',
    controller: $scope => $scope.warning = warning
})

warning $scope,

<md-dialog>
    <md-dialog-content>
        <span> {{warning}} </span>
    <md-dialog-content>
<md-dialog>
+1

Source: https://habr.com/ru/post/1652172/


All Articles