0

Why is the WizardPageSchoolclassCodesFactory factory not injected? Its always null!

The error in my chrome console is this:

Error: [$injector:unpr] Unknown provider: $scopeProvider <- $scope <- WizardPageSchoolclassCodesFactory



    angular.module('myModule').controller('WizardMainController', function ($scope, WizardPageSchoolclassCodesFactory) {

    // do stuff with data

    });

'use strict';
angular.module('myModule').factory('WizardPageSchoolclassCodesFactory', function($scope) {
        this.getData = function()
        {
            return "hello";
        }
});
5
  • Remove injection of $scope from factory definition and create an API in the factory. Commented Jul 18, 2014 at 21:47
  • Why can I not inject the scope into the factory? I removed the $scope but nothing changed! Commented Jul 18, 2014 at 21:48
  • Refer this question. Commented Jul 18, 2014 at 21:52
  • Sorry remove $scope helped, I just got another error which was misleading! Commented Jul 18, 2014 at 21:52
  • 3
    Your factory is declared like a service. A factory should be a function that returns an object. Commented Jul 18, 2014 at 21:53

1 Answer 1

0

If you want to pass scope into your factory you can do it in this way:

app.factory('WizardPageSchoolclassCodesFactory', function() {
    return function(scope){
        this.getData = function()
        {
            scope.test = "test";
            return "hello";
        }
    }
});

In controller you have to create new instance of your factory and pass scope in constructor:

var factory = new WizardPageSchoolclassCodesFactory($scope);

Then you can use declared factory methods:

factory.getData();

http://jsfiddle.net/aartek/THE4P/

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

4 Comments

Surely I do NOT want to bring the $scope outside a controller. I just wanted to know the technical reason why I get an 'exception' doing this...
This way you are tightly coupling the factory with the controller. Factory should be abstracted from the controller logic. You can send the variable test to factory instead of sending the scope altogether. Makes sense?
@dmahapatro you're right, but sometimes passing scope has advantages. For example recently I had to create widgets dashboard - each widget has it own scope and widgets should communicate with each other by sending/receiving events. Instead of repeating same code in each controller, I inject factory with passed scope, and this factory creates listeners for me. It looks like this: pastebin.com/xMGZha2v. Instead of writing scope.$on... I can simply register listeners to scope by using factory methods.
Passing controller scope in a factory is a bad practice... It is a clear violation of encapsulation IMHO.

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.