Niranjan Godbole
Niranjan Godbole

Reputation: 2175

How to inject factory to controller?

Hi I am new to angularjs and I am trying to inject some factory to controller but I am facing difficulties. My factory works fine. I am able to set data in factory.

Below is my code where I inject factory.

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

This is my factory code.

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

I set data in some other controller with SomeFactory.setData("123")

I want to inject someFactory as below:

RegistrationOTPVerification.$inject = ['SomeFactory'];

But whenever I write this, I get error RegistrationOTPVerification is undefined. If I comment that line everything works fine but I want to get some data from factory.

My factory name is SomeFactory and I want to inject in above controller. Any help would be appreciated.

Upvotes: 1

Views: 1783

Answers (2)

tanmay
tanmay

Reputation: 7911

Firstly, you missed CONSTANTS in your injections.

Next, I think you are mixing two kinds of syntax for controller here. Either use anonymous function or named. Don't mix both together.

Here's how your code should look like.

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

Upvotes: 2

dfsq
dfsq

Reputation: 193301

You missed CONSTANTS in controller dependency list:

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

So whatever you inject SomeFactory is available in controller under the name CONSTANTS and symbol SomeFactory is undefined.

Upvotes: 2

Related Questions