0

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.

8
  • where the "FirstController" is called? Commented Apr 25, 2016 at 16:53
  • I have a very basic HTML file <body ng-app="testapp"> <div ng-controller="FirstController"> </div> </body> Commented Apr 25, 2016 at 16:55
  • @nrdb Here is a working JSFiddle. Commented Apr 25, 2016 at 16:56
  • Ok, after fixing a typo with the controller name, i am getting a different error: Unknown provider: $scopeProvider <- $scope <- commonService Commented Apr 25, 2016 at 16:57
  • @IgorRaush thanks for that fiddle, i am no longer getting errors, i can see the console.log() but it thinks the two functions in the factory are undefined Commented Apr 25, 2016 at 17:06

1 Answer 1

1

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.

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

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.