Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ui bootstrap modal's controller 'is not defined'

i am trying to use the modal directive from ui-bootstrap 0.6

here is the working default plunker from the ui-bootstrap page:

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

now, i tried to make the coding style fits angular-seed style to include it in one app like this :

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');
    };
}]);

but it's giving an error ReferenceError: ModalInstanceCtrl is not defined

how can i make this work using this way of defining controllers ?

like image 508
François Romain Avatar asked Oct 17 '13 16:10

François Romain


2 Answers

Provide controller name as String, exactly as you would do in route definitions, directives etc.:

var modalInstance = $modal.open({
        templateUrl: 'myModalContent.html',
        controller: 'ModalInstanceCtrl',
        resolve: {
            items: function () {
            return $scope.items;
            }
        }
    });
like image 171
pkozlowski.opensource Avatar answered Nov 16 '22 15:11

pkozlowski.opensource


You can use quotes as the other answer suggests, or you can also do as the example in the docs and define the variable:

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

like image 1
Foo L Avatar answered Nov 16 '22 16:11

Foo L