0

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

1
  • If you use the [] brackets for minification, you have to explicitly label each dependency. ["$scope", "$http", function($scope, $http)... Commented Sep 23, 2013 at 16:39

2 Answers 2

2

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

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

Comments

0

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)

Comments

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.