ladar
ladar

Reputation: 5876

AngularJS - configure services and $routeProvider

This is quite dummy question, but I can't figure it out. I have some simple module, configure like this:

var synergy_module = angular.module('synergy', [])
    .config(function($provide, $routeProvider) {
$routeProvider.when('/s/:id', {templateUrl: 'partials/...', controller: SpecPoolCtrl});
$routeProvider.when('/s', {templateUrl: 'partials/...', controller: SpecPoolCtrl});
$provide.factory('foo', function(){return 1;});    
});

Now in SpecPoolCtrl:

function SpecificationCtrl($scope, foo,$http, $location, $routeParams) {
   window.console.log($scope.foo());
   ...
}

When I run this code, I'm getting

TypeError: Object #<Object> has no method 'foo'
at new SpecificationCtrl (app/js/controllers.js:373:31)
at invoke (app/lib/angular/angular.js:2795:28)
at Object.instantiate (app/lib/angular/angular.js:2805:23)
at $get (app/lib/angular/angular.js:4621:24)

Can you help me what's wrong? Or some related example how to make it work?

Upvotes: 1

Views: 2109

Answers (1)

maxisam
maxisam

Reputation: 22705

You should use it like

$provide.factory('foo', function(){
  return 1;
});

function SpecificationCtrl($scope, foo,$http, $location, $routeParams) {
    console.log(foo);
   ...
}

This is because foo isn't defined in your SpecificationCtrl $scope, it's being injected so you can use it by just writing foo as mentioned above.

Also, if you were to minify your code, please consider injection array syntax.

Here is the plunker

Upvotes: 1

Related Questions