0

I am trying to develop some Angular.js webapp. In that app I would like to use angular-ui.bootstrap. So i was trying to do so and here is my app.js code. My question is, why do I get error like

reservationController not a function got undefined

if I use the recommended

(function() { })();

at the start of my app.js file?

(function() {
  var app = angular.module('app', ['ngGrid', 'ui.bootstrap']);

  var reservationController = function ($scope, $modal, $log) {
    $scope.items = ['item1', 'item2', 'item3'];
    $scope.open = function (size) {
      var modalInstance = $modal.open({
        templateUrl: 'reservationTemplate.html',
        controller: ModalInstanceCtrl,
        size: size,
        resolve: {
          items: function () {
            return $scope.items;
          }
        }
      });
      modalInstance.result.then(function (selectedItem) {
        $log.info('Foo');
      }, function () {
        $log.info('Modal dismissed at: ' + new Date());
      });
    };
  };

// Please note that $modalInstance represents a modal window (instance) dependency.
// It is not the same as the $modal service used above.

var ModalInstanceCtrl = function ($scope, $modalInstance, items) {
  $scope.items = items;
  $scope.selected = {
    item: $scope.items[0]
  };
  $scope.ok = function () {
    $modalInstance.close();
  };

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

})();

Here is hte url to the planker planker - the app

witrin
  • 3,701
  • 1
  • 24
  • 49
Alex
  • 35
  • 7
  • Which version of angular you are using? If using 1.3x [this answer might help](http://stackoverflow.com/questions/25111831/controller-not-a-function-got-undefined-while-defining-controllers-globally/25111942#25111942) – PSL Aug 05 '14 at 02:43
  • Hello. I am using AngularJS v1.2.21. I just figured out, that if I remove the "(function() { })();" at the begin and the end of my js file, it works. Otherwise, if I leave the controller name without the "var" keyword it works too. But I cant explain it to myself, so I can figure it out, whats going on here. – Alex Aug 05 '14 at 02:54
  • That is because when you omit `var` keyword it becomes a part of global object and angular 1.2 looks at the global object as well to fetch the controller, which will break in 1.3. Recommended method is to write the controllers in a closure (as you have done already) and register it using `.controller` syntax, so you dont pollute globals. – PSL Aug 05 '14 at 02:55
  • Ok, thank you for your help. I sounds useful to know it. I would like to ask one question about how to use the angluar-ui.bootstrap "components" right? Should I register the controllers using .controller syntax or should I use it like its says on the angular-ui website? – Alex Aug 05 '14 at 03:15
  • in case of controllers for modal, you should be good (i guess) because ui bootstrap handles it by looking at the function reference that you are providing as the constructor for the controller in the very next line... – PSL Aug 05 '14 at 03:18
  • hehe, alright. sounds a bit confused, but Ill just try to figure it out. Thanks =) – Alex Aug 05 '14 at 03:31
  • maybe you are missing `ng-app`? i tried a little bit arround, but then some other errors appeared: http://plnkr.co/edit/IxkrbcDfOTxRqSxKhTRq?p=preview – Raphael Müller Aug 05 '14 at 15:01
  • Hello! The body tag already contains the ng-app directive. I've seen you declared the controllers with the .controller() angularjs method. It should work, but were trying to inject the "ModalInstanceCtrl" in the "reservationController". It is wrong because as I know you are able only to inject services. Leaving "ModalInstanceCtrl" helps in your app. – Alex Aug 05 '14 at 15:15

0 Answers0