无法将工厂注入angularjs控制器

时间:2014-07-18 21:44:48

标签: angularjs

为什么没有注入WizardPageSchoolclassCodesFactory工厂?它永远是空的!

我的Chrome控制台中的错误是:

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

1 个答案:

答案 0 :(得分:0)

如果您想将范围传递到工厂,可以这样做:

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

在控制器中,您必须创建工厂的新实例并在构造函数中传递范围:

var factory = new WizardPageSchoolclassCodesFactory($scope);

然后您可以使用声明的工厂方法:

factory.getData();

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

相关问题