【问题标题】:How to inject factory to controller?如何将工厂注入控制器?
【发布时间】:2017-04-08 15:45:36
【问题描述】:

您好,我是 angularjs 的新手,我正在尝试向控制器注入一些工厂,但我遇到了困难。我的工厂工作正常。我可以在工厂设置数据。

下面是我注入工厂的代码。

function () {
    angular.module('RoslpApp').controller('RegistrationOTPVerification', ['$scope', '$http', '$translatePartialLoader', '$translate', '$state', '$stateParams', 'SomeFactory',
        function ($scope, $http, $translatePartialLoader, $translate, $state, $stateParams, CONSTANTS, SomeFactory) {

        var OTP = $stateParams.pageList.OTP;
        $scope.EnterOTP = "Please enter this OTP to verify the user " + OTP;
      //RegistrationOTPVerification.$inject = ['SomeFactory'];
      //Facing issues in above line
        alert(1);
        function RegistrationOTPVerification(SomeFactory) {
            var vm = this;
            vm.someVariable = SomeFactory.getData();
            console.log(vm.someVariable);  // logs your data
            alert(vm.someVariable);
        }
});

这是我的工厂代码。

(function () {
    'use strict';
    angular
      .module('RoslpApp')
      .factory('SomeFactory', SomeFactory);

    SomeFactory.$inject = [];

    function SomeFactory() {
        var someData;

        var factory = {
            setData: setData,
            getData: getData
        };
        function setData(data) {

            someData = data;
        }

        function getData() {
            return someData;
        }

        return factory;
    }
})();

我用SomeFactory.setData("123")在其他控制器中设置数据

我想注入someFactory如下:

RegistrationOTPVerification.$inject = ['SomeFactory'];

但是每当我写这个,我得到错误RegistrationOTPVerification is undefined。如果我评论该行一切正常,但我想从工厂获取一些数据。

我的工厂名称是SomeFactory,我想注入上面的控制器。任何帮助将不胜感激。

【问题讨论】:

  • 也分享你的工厂代码并说明到底出了什么问题

标签: angularjs dependency-injection


【解决方案1】:

首先,您在注射时错过了CONSTANTS

接下来,我认为您在这里混合了controller 的两种语法。使用匿名函数或命名。不要将两者混合在一起。

你的代码应该是这样的。

function () {
    angular.module('RoslpApp').controller('RegistrationOTPVerification', ['$scope', '$http', '$translatePartialLoader', '$translate', '$state', '$stateParams', 'SomeFactory',
        function ($scope, $http, $translatePartialLoader, $translate, $state, $stateParams, SomeFactory) {

        var vm = this;
        var OTP = $stateParams.pageList.OTP;
        vm.EnterOTP = "Please enter this OTP to verify the user " + OTP;
        vm.someVariable = SomeFactory.getData();
        console.log(vm.someVariable);  // logs your data
        alert(vm.someVariable);
});

【讨论】:

    【解决方案2】:

    你错过了控制器依赖列表中的 CONSTANTS:

       '$translate', '$state', '$stateParams', 'SomeFactory',
    ... $translate,   $state,   $stateParams,   CONSTANTS,    SomeFactory) {
    

    因此,无论您注入 SomeFactory 什么,都可以在控制器中以名称 CONSTANTS 使用,而符号 SomeFactory 未定义。

    【讨论】:

    • 谢谢。 angular.module('RoslpApp').controller('RegistrationOTPVerification', ['$scope', '$http', '$translatePartialLoader', '$translate', '$state', '$stateParams', 'CONSTANTS', 'SomeFactory', 函数 ($scope, $http, $translatePartialLoader, $translate, $state, $stateParams, CONSTANTS, SomeFactory)
    猜你喜欢
    • 1970-01-01
    • 2017-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多