2

I trying to get on my feet with angular.js using the seed app.

When the browser executes my controller i get the following error:

TypeError: Cannot set property 'players' of undefined
at new bankC (http://localhost:8888/monopoly/js/controllers.js:18:20)
at invoke (http://localhost:8888/monopoly/lib/angular/angular.js:2795:28)
at Object.instantiate (http://localhost:8888/monopoly/lib/angular/angular.js:2805:23)
at http://localhost:8888/monopoly/lib/angular/angular.js:4620:24
at update (http://localhost:8888/monopoly/lib/angular/angular.js:13692:26)
at http://localhost:8888/monopoly/lib/angular/angular.js:8002:24
at Array.forEach (native)
at forEach (http://localhost:8888/monopoly/lib/angular/angular.js:110:11)
at Object.Scope.$broadcast (http://localhost:8888/monopoly/lib/angular/angular.js:8000:11)
at http://localhost:8888/monopoly/lib/angular/angular.js:7185:26 

this is the controller code which is the controller.js file

function bankC($scope) {
  $scope.players = [
      {
        id: 0,
        name: "Playe1",
        balance: 1500
      },
      {
        id: 1,
        name: "Player2",
        balance: 1500
      },
      {
        id: 2,
        name: "Player 3",
        balance: 1500
      }
  ];
}
bankC.$inject = [];

Thanks

EDIT:

its registered with angular js to be used when on a certain "page"

angular.module('myApp', ['myApp.filters', 'myApp.services', 'myApp.directives']).
config(['$routeProvider', function ($routeProvider) {
    $routeProvider.when('/view1', {templateUrl: 'partials/partial1.html', controller: MyCtrl1});
    $routeProvider.when('/view2', {templateUrl: 'partials/partial2.html', controller: MyCtrl2});
    $routeProvider.when('/bank', {templateUrl: 'partials/bank.html', controller: bankC});
    $routeProvider.otherwise({redirectTo: '/bank'});
}]);

EDIT2:

Removing "bankC.$inject = [];" from the controller file solved the problem, so why does the angular.js seed app include it?

4
  • are you sure that you're calling the function with a defined parameter, not undefined? Commented Aug 7, 2012 at 13:47
  • how would i define the parameter in angularjs? This may be where im going wrong.... Commented Aug 7, 2012 at 13:52
  • how is the bankC function called? Commented Aug 7, 2012 at 13:56
  • see edit, i've added where its registered Commented Aug 7, 2012 at 14:04

1 Answer 1

9

You are getting that error because of the last line

bankC.$inject = [];

This tells the angular injector to inject nothing into the controller while the controller is looking for $scope.

If you change this to

bankC.$inject = ['$scope'];

it should work fine.

That last line exists because angular uses Dependancy Injection. Angular looks for variables with name $scope when you request for it in a controller. But if the code is minified then the obfuscation will change the name of $scope to something else. In order to maintain sanity when this happens that last line is introduced. When ever you declare a new controller the best practice would be to include that last line with all the variables you want angulars DI to inject into that controller.

Note : If you are interested in getting more things injected into that controller ( or similiar controllers ) then you will have to update bankC.$inject as well.

Sign up to request clarification or add additional context in comments.

7 Comments

Should angularjs automatically do this as i have registered that controller with that url page?
Angular automatically calls / instantiates the corresponding controller. It would be good if you provide your html or your app.js where you would have referenced bankC.
im using partials so the controller isnt registered in any HTML, it is registered in the app.js which i've added to my original question.
any idea why this fixed it? It seems odd that the seed app came with this code that once removed fixed my problem.
@Cooltrooper That last line exists because angular uses Dependancy Injection. Angular looks for variables with name $scope when you request for it in a controller. But if the code is minified then the obfuscation will change the name of $scope to something else. In order to maintain sanity when this happens that last line is introduced. When ever you declare a new controller the best practice would be to include that last line with all the variables you want angulars DI to inject into that controller.
|

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.