ui bootstrap modal的控制器'未定义'

时间:2013-10-17 16:45:22

标签: angularjs angular-ui-bootstrap

我正在尝试使用ui-bootstrap 0.6的模态指令

这是ui-bootstrap页面中的工作默认plunker:

http://plnkr.co/edit/JGBiBSeRqOnwRhYA9py8?p=preview

现在,我尝试使编码样式适合angular-seed样式,将其包含在这样的应用程序中:

http://plnkr.co/edit/Y59rwlcNpQdijKtmjOPy?p=preview

angular.module('MyModal', ['ui.bootstrap', 'MyModal.controllers']);

angular.module('MyModal.controllers', [])
    .controller('ModalDemoCtrl', [ '$scope', '$modal', '$log', function ($scope, $modal, $log) {
    $scope.items = ['item1', 'item2', 'item3'];

    $scope.open = function () {
        var modalInstance = $modal.open({
            templateUrl: 'myModalContent.html',
            controller: ModalInstanceCtrl,
            resolve: {
                items: function () {
                return $scope.items;
                }
            }
        });

        modalInstance.result.then(function (selectedItem) {
            $scope.selected = selectedItem;
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    };
}])
.controller('ModalInstanceCtrl', [ '$scope', '$modalInstance', 'items', function ($scope, $modalInstance, items) {
    $scope.items = items;
    $scope.selected = {
        item: $scope.items[0]
    };

    $scope.ok = function () {
        $modalInstance.close($scope.selected.item);
    };

    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
}]);

但它发出了错误ReferenceError: ModalInstanceCtrl is not defined

如何使用这种定义控制器的方式来完成这项工作?

2 个答案:

答案 0 :(得分:51)

将控制器名称提供为 String ,与路径定义,指令等完全相同:

var modalInstance = $modal.open({
        templateUrl: 'myModalContent.html',
        controller: 'ModalInstanceCtrl',
        resolve: {
            items: function () {
            return $scope.items;
            }
        }
    });

答案 1 :(得分:1)

您可以使用引号作为另一个答案建议,或者您也可以作为文档中的示例并定义变量:

var ModalInstanceCtrl = function ($scope, $modalInstance, items) { ... }