错误:[$ controller:ctrlreg]未注册名为“AppCtrl”的控制器

时间:2017-10-31 03:23:09

标签: angularjs angular-material

angular.module('MyApp',['ngMaterial','ngMessages','material.svgAssetsCache'])

.controller('AppCtrl', function ($scope, $mdDialog, ) {


    $scope.showAdvanced = function (ev) {
        $mdDialog.show({
            controller: DialogController,
            templateUrl: 'dialog1.tmpl.html',
            parent: angular.element(document.body),
            targetEvent: ev,
            clickOutsideToClose: true
        })

    };

    function DialogController($scope, $mdDialog) {
        $scope.hide = function () {
            $mdDialog.hide();
        };

        $scope.cancel = function () {
            $mdDialog.cancel();
        };

        $scope.answer = function (answer) {
            $mdDialog.hide(answer);
        };
    }
});

出现错误 有谁可以帮忙解决?

1 个答案:

答案 0 :(得分:0)

您必须注册DialogController才能使用它,并将其作为string的名称引用提供给AppCtrl。

您的代码应该像:

angular.module('MyApp', ['ngMaterial', 'ngMessages', 'material.svgAssetsCache'])
   .controller('AppCtrl', function ($scope, $mdDialog) {
      $scope.showAdvanced = function (ev) {
          $mdDialog.show({
            controller: 'DialogController', // notice single qoutes here
            templateUrl: 'dialog1.tmpl.html',
            parent: angular.element(document.body),
            targetEvent: ev,
            clickOutsideToClose: true
        })    
     }) // end of 'AppCtrl'
   .controller('DialogController', function ($scope, $mdDialog) {
        $scope.hide = function () {
            $mdDialog.hide();
        };

        $scope.cancel = function () {
            $mdDialog.cancel();
        };

        $scope.answer = function (answer) {
            $mdDialog.hide(answer);
        };
    }); // end of 'DialogController'
相关问题