46

I'm trying to use the angular-seed template with the default settings. In controllers.js I use

angular.module('myApp.controllers', []).
  controller('MyCtrl1', [function($scope) {
      $scope.test = 'scope found!';
  }])
  .controller('MyCtrl2', [function() {

  }]);

There the $scope is always undefined. When I take the controller out of the module and register it globally it works fine. As here:

function MyCtrl1($scope) {
    $scope.test = "scope found!";
}
MyCtrl1.$inject = ['$scope'];

Could someone explain to me why this is?

4 Answers 4

67

You cannot mix things like that. You need to decide on one of the two possibilities:

app = angular.module('test', []);

// possibility 1 - this is not safe for minification because changing the name
// of $scope will break Angular's dependency injection
app.controller('MyController1', function($scope) {
    // ...
});

// possibility 2 - safe for minification, uses 'sc' as an alias for $scope
app.controller('MyController1', ['$scope', function(sc) {
    // ...
}]);

I would not advise using the other syntax which declares Controller directly. Sooner or later with the growth of you app it will become hard to maintain and keep track. But if you must, there are 3 possibilities:

function myController1 = function($scope) {
    // not safe for minification
}

function myController2 = ['$scope', function(sc) {
    // safe for minification, you could even rename scope
}]

var myController3 = function(sc) {
    // safe for minification, but might be hard
    // to read if controller code gets longer
}
myController3.$inject = ['$scope'];
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for explaining. I still want to know how to get reference to $scope using the default syntax that google provided in their template: angular.module('myApp.controllers', []). controller('MyCtrl1', [function() { }]) .controller('MyCtrl2', [function() { }]);
@AshrafFayad The only way to get a reference to $scope is to define a controller in one of the above mentioned ways.
Not entirely true, in fact you left out what I think is the most preferred way of doing it.
great explanation of the different ways to implement it. Bonus points for taking minification into account
Thanks - I never understood this. God I hate angularJS
|
17

This is the proper way:

angular.module('myApp.controllers', []);

angular.module('myApp.controllers').controller('MyCtrl1', ['$scope', function($scope) {

}]);

Comments

1

I was also searching for that one, it seems that you need to type '$scope' before the function, as below:

    angular.module('myApp.controllers', []).
  controller('MyCtrl1', ['$scope', function($scope) {
      $scope.test = 'scope found!';
  }])
  .controller('MyCtrl2', ['$scope',function() {

  }]);

It kinda makes sense, I think it should be more clear though..

Comments

-1

You can simply remove '[' and ']' when You are using $scope.

angular.module('myApp.controllers', []).
controller('MyCtrl1', function($scope) {
    $scope.test = 'scope found!';
  })
  .controller('MyCtrl2', [
    function() {

    }
  ]);

1 Comment

You cannot simply remove [ ] when using scope (or any other one, for that matter). You can do it, if you don't care about minification, which is something entirely different.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.