ndrb
ndrb

Reputation: 101

Using an AngularJS factory and controller gives undefined error

I have a very basic factory and controller in AngularJS, took it from another post on Stack Overflow

var app = angular.module( 'testapp', [] );
app.factory('commonService', function ($scope) {
  var obj= {};
  obj.func = function () {
    console.log('route 1');
  }
  obj.func1 = function () {
    console.log('route 2');
  }
  return obj;
});
app.controller('FirstController', function ($scope, commonService) { 
  console.log('route 1' + commonService.func());  
});
app.controller('SecondController', function ($scope, commonService) { 
  console.log('route 2' + commonService.func1());  
});

For some reason this keep giving me the error Unknown provider: $scopeProvider <- $scope <- commonService

I am trying to use a factory in order to clean up my code and re-use some functions in my controller; I have tried using a service and had the same results.

Upvotes: 0

Views: 216

Answers (1)

sebenalern
sebenalern

Reputation: 2561

The problem is you are injecting $scope into the factory but it cannot access your $scope. Also it doesn't make much sense to pass $scope into your factory. Take look at this.

Upvotes: 1

Related Questions