25

我正在尝试使用 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

我怎样才能使用这种定义控制器的方式来完成这项工作?

4

2 回答 2

50

提供控制器名称为String,就像您在路由定义、指令等中所做的那样:

var modalInstance = $modal.open({
        templateUrl: 'myModalContent.html',
        controller: 'ModalInstanceCtrl',
        resolve: {
            items: function () {
            return $scope.items;
            }
        }
    });
于 2013-10-17T17:02:54.587 回答
1

您可以按照其他答案的建议使用引号,也可以按照文档中的示例进行操作并定义变量:

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

于 2013-10-17T17:04:22.270 回答