angularjs对话框似乎对我不起作用

时间:2015-04-28 22:02:46

标签: javascript angularjs dialog material

我一直在尝试在我的应用程序中实现AngularJS对话框,我从material.angularjs.org中提供的演示中复制了所有内容但是我似乎无法使其正常工作。谁能看到我错过了什么?

这是Javascript代码:

  // TABLE CONTROLLS GO HERE 
app.controller('AddTableController',['$scope', function($scope,$mdDialog){
        $scope.allTables = tables;
        //method to add tables 
        $scope.showAdvanced= function(ev) {
            $mdDialog.show({
              controller:DialogController,
              templateUrl:'index_directives/dialog-add-table.html',
            })
        };
}]);
//HELPER METHOD FOR THE AddTableController//////////////////////////////
function DialogController($scope, $mdDialog) {
  $scope.hide = function() {
    $mdDialog.hide();
  };
  $scope.cancel = function() {
    $mdDialog.cancel();
  };
  $scope.answer = function(answer) {
    $mdDialog.hide(answer);
  };
}

然后在按钮点击时调用该函数:

<md-button class="md-fab add-button" ng-click="showAlert($event)">+</md-button>

从控制台的错误日志中得到类似的内容:

TypeError: Cannot read property 'show' of undefined
    at n.app.controller.$scope.showAlert (http://localhost/angular/js/app.js:24:16)
    at ib.functionCall (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:199:303)
    at Ec.(anonymous function).compile.d.on.f (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:216:74)
    at n.$get.n.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:126:15)
    at n.$get.n.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:126:241)

1 个答案:

答案 0 :(得分:3)

您忘了定义$ mdDialog依赖项:

app.controller('AddTableController',['$scope', '$mdDialog', function($scope,$mdDialog){

由于忘记定义依赖注入非常常见,因此您可能对@Michael Benford关于ng-annotate的this suggestion感兴趣。