简体   繁体   中英

Can not inject factory into angularjs controller

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";
        }
});

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM