user2774727
user2774727

Reputation: 1

Getting $Scope in angular.module.controller

This is a noobie question:

I am playing with the Angular seed app and am trying to write a controller but am having no luck getting access to $scope (and any other dependency).

angular.module('myApp.controllers', []).
  controller('mainCtrl', [function( $scope, $http ) {
      $http.get('config/configuration.json').success( function( data ) {
        $scope.gametitles   = data.gametitles;
        $scope.environments = data.environments;
        $scope.playermanagerServer = data.playermanagerServer;
      });
      $scope.gametitle = $scope.gametitles[0];
      $scope.environment = $scope.environments[0];      
  }])

If I break in the code, both $http and $scope are undefined. How do I get access to these?

Thanks in advance

Upvotes: 0

Views: 265

Answers (2)

aabes
aabes

Reputation: 208

Or,assuming you're not uglifying your code you could just use simpler injection formation:

controller('mainCtrl', function( $scope, $http ) {} )

(note that the function is not a member of an array, but passed directly)

Upvotes: 0

tetotechy
tetotechy

Reputation: 735

I think the problem is in the syntax of your controller declaration. Try controller('mainCtrl', ['$scope', '$http', function ($scope, $http) { ... controller code ...}]);

you may want to check A's documentation for dependency injection in controllers here

Upvotes: 2

Related Questions