0

I have a MyResource service defined like this:

angular.module('resourceApp')
  .factory('MyResource', ['$resource', function($resource) {
      return $resource('/data');
}]);

And then I have a controller which uses MyResource as a dependancy:

angular.module('resourceApp')
  .controller('MainCtrl', ['MyResource', function($scope, MyResource) {
    $scope.data = MyResource.get(); // <-- this is where the error occurs
}]);

When I define the dependancy like above, using an Inline Array Annotation, I get an error "MyResource is undefined" at the line marked with the comment.

But if I change the syntax to Implicit Annotation:

angular.module('resourceApp')
  .controller('MainCtrl', function($scope, MyResource) {
    $scope.data = MyResource.get();
});

I somehow magically get things working!

The question is: what's wrong with the first one? I could leave the implicit annotation, but the documentation says that it won't survive the minification.

2 Answers 2

1

You are forgetting the $scope in the first one, it should be:

anguar.module('app').controller('CTRL', ['$scope', 'MyService', function($scope, Service)

Currently you have no scope, and the $scope variable actually points to the service

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

1 Comment

Yes! I tried including '$scope', but put it after 'MyService'! That's why it didn't work.
1

You forgot to specify the $scope in your array:

.controller('MainCtrl', ['$scope', 'MyResource', function($scope, MyResource) {

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.