javascriptangularjsdialogangularjs-material

The angularjs dialog doesn't seem to work for me


I have been trying to implement AngularJS dialog in my app and i copy everything from the demos provided in material.angularjs.org however I don't seem to be able to make it work. Could anyone see what am I missing ?

This is the Javascript code:

  // 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);
  };
}

Then the function is called on button click :

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

From the error log in the console I get something like this:

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)

Solution

  • You forgot to define the $mdDialog dependency:

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

    Since it's very common forgetting to define the dependency injection, you may be interested on this suggestion of @Michael Benford regarding ng-annotate.