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

问题描述 投票:1回答:1

我一直在尝试在我的应用程序中实现AngularJS对话框,我复制了所有的demo,在这里,我可以看到我的应用程序中的对话框。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)
javascript angularjs dialog angularjs-material
1个回答
3
投票

你忘了定义$mdDialog依赖关系。

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

由于忘记定义依赖注入是很常见的事情,你可能会对以下内容感兴趣 这个建议 的 @Michael Benford 关于 ng-annotate。

© www.soinside.com 2019 - 2024. All rights reserved.