javascript - 在 Angular Material 中为 mdDialog Controller 设置数组类型语法

标签 javascript angularjs angular-material

我正在尝试使用 Angular Material 框架在网络上使用 Material Design。我正在使用 $mdDialog 服务,它需要一个 Controller 属性来创建自定义对话框。 Angular Material 不遵循 Controller 定义的数组类型语法,这会在缩小时中断。我这里有以下代码:

HTML

<div ng-controller="AppCtrl as appCtrl" class="md-padding" id="popupContainer">
  <p class="inset">
    Open a dialog over the app's content. Press escape or click outside
    to close the dialog and send focus back to the triggering button.
  </p>
  <div class="dialog-demo-content" layout="row" layout-wrap >
    <md-button class="md-primary md-raised" ng-click="appCtrl.showAdvanced($event)" flex flex-md="100">
      Custom Dialog
    </md-button>
  </div>
</div>

JS

angular.module('dialogDemo1', ['ngMaterial'])
.controller('AppCtrl', ['$mdDialog', function($mdDialog) {
  var self = this;
  self.showAdvanced = function(ev) {
    $mdDialog.show({
      controller: DialogController,
      templateUrl: 'dialog1.tmpl.html',
      parent: angular.element(document.body),
      targetEvent: ev,
      clickOutsideToClose:true
    })
    .then(function(answer) {
      // Do something on success
    }, function() {
      // Do something on failure
    });
  };
}]);
function DialogController($scope, $mdDialog) {
  $scope.hide = function() {
    $mdDialog.hide();
  };
  $scope.cancel = function() {
    $mdDialog.cancel();
  };
  $scope.answer = function(answer) {
    $mdDialog.hide(answer);
  };
}

有人尝试过并可以帮助我吗?

最佳答案

每个 Angular Controller 都遵循数组类型语法。您只需将 Controller 更改为...

angular.module('dialogDemo1', ['ngMaterial'])
.controller('AppCtrl', ['$mdDialog', function($mdDialog) {
  var self = this;
  self.showAdvanced = function(ev) {
    $mdDialog.show({
      controller: 'DialogController',
      templateUrl: 'dialog1.tmpl.html',
      parent: angular.element(document.body),
      targetEvent: ev,
      clickOutsideToClose:true
    })
    .then(function(answer) {
      // Do something on success
    }, function() {
      // Do something on failure
    });
  };
}]).controller('DialogController', ['$scope', '$mdDialog', function($scope, $mdDialog) {
      $scope.hide = function() {
        $mdDialog.hide();
      };
      $scope.cancel = function() {
        $mdDialog.cancel();
      };
      $scope.answer = function(answer) {
        $mdDialog.hide(answer);
      };
    }]);

关于javascript - 在 Angular Material 中为 mdDialog Controller 设置数组类型语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32353183/

相关文章:

javascript - 如何在我的 Controller 中调用一个函数,该函数也被用作 View 的范围

html - 如何在特定索引处的 ngfor 中插入/显示 div

angular - 使用 [(ngModel)] 与 HTML 中的字符串值绑定(bind) <mat slide toggle>

javascript - 如何验证一张大表格

javascript - jquery。 livequery() 奇怪的行为

css - 如何将焦点添加到表格行 <tr>?

javascript - 状态更改时 URL 不更新

javascript - Angular 的 $mdDialog 中使用了什么范围

javascript - 正则表达式匹配sql创建表语句中的表名

javascript - 过滤其中包含另一个数组的数组