user6934713 user6934713 - 27 days ago 19
Javascript Question

Angular material $mdDialog with second place holder

I am trying to add a second input to an angular material dialog, but I am not sure if it is possible. I tried to add the Test2 placeholder but didn't work. Thank you for the help in advance.

$scope.showPrompt = function (ev) {
$scope.statusTopic = '';
var confirm = $mdDialog.prompt()
.title('Test?')
.placeholder('Test1')
//.placeholder('Test2')
.targetEvent(ev)
.ok('Okay!')
.cancel('Cancel');

Answer

Here's an example using Custom Dialog - CodePen

Markup

<div ng-controller="MyController as vm" ng-cloak="" ng-app="app">
  <md-button class="md-primary md-raised" ng-click="vm.open($event)">
    Custom Dialog
  </md-button>
  <div ng-if="vm.showText" layout="column">
    {{vm.placeholder1}}
    <br>
    {{vm.placeholder2}}
  </div>

  <script type="text/ng-template" id="test.html">
    <md-dialog aria-label="Test">
        <div layout-padding layout="column">
        <md-input-container>
            <label>Placeholder 1</label>
          <input ng-model="vm.placeholder1">
        </md-input-container>
        <md-input-container>
            <label>Placeholder 2</label>
          <input ng-model="vm.placeholder2">
        </md-input-container>
        <md-button ng-click="vm.save()" class="md-primary">Save</md-button>
      </div>
    </md-dialog>
  </script>
</div>

JS

angular.module('app',['ngMaterial'])

.controller('MyController', function($scope, $mdDialog) {
  this.open = function(ev) {
    this.showText = false;
    $mdDialog.show(
      {
        templateUrl: "test.html",
        clickOutsideToClose: true,
        scope: $scope,
        preserveScope: true,
        controller: function($scope) {
       },
    });
  };

  this.save = function () {
    this.showText = true;
    $mdDialog.cancel();
  }
})

Demo:

https://material.angularjs.org/latest/demo/input

Docs:

https://material.angularjs.org/latest/api/directive/mdDialog https://material.angularjs.org/latest/api/service/$mdDialog

Comments