Click here to Skip to main content
15,921,203 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am very new to AngularJs.
while injecting factory we are getting unknown provider error. For form posting i refered this


Version: AngularJs 1.5.5

Below my controller and factory. Can anyone help me to reolve.

What I have tried:

//Factory
angular.module('transformRequestService', [])
    .factory('transformRequestAsFormAsPost',
        function () {
            // I prepare the request data for the form post.
            function transformRequest(data, getHeaders) {
                var headers = getHeaders();
                headers["Content-type"] = "application/x-www-form-urlencoded; charset=utf-8";
                return (serializeData(data));
            }
            // Return the factory value.
            return (transformRequest);
            // ---
            // PRVIATE METHODS.
            // ---
            // I serialize the given Object into a key-value pair string. This
            // method expects an object and will default to the toString() method.
            // --
            // NOTE: This is an atered version of the jQuery.param() method which
            // will serialize a data collection for Form posting.
            // --
            // https://github.com/jquery/jquery/blob/master/src/serialize.js#L45
            function serializeData(data) {
                // If this is not an object, defer to native stringification.
                if (!angular.isObject(data)) {
                    return ((data == null) ? "" : data.toString());
                }
                var buffer = [];
                // Serialize each key in the object.
                for (var name in data) {
                    if (!data.hasOwnProperty(name)) {
                        continue;
                    }
                    var value = data[name];
                    buffer.push(
                        encodeURIComponent(name) +
                        "=" +
                        encodeURIComponent((value == null) ? "" : value)
                    );
                }
                // Serialize the buffer and clean it up for transportation.
                var source = buffer
                    .join("&")
                    .replace(/%20/g, "+")
                ;
                return (source);
            }
        });


//App.js
var routerApp = angular.module('myApp', ['ui.router'
, 'vendorLoginModule'
, 'ui.bootstrap'
, 'createUser'
, 'ui.grid'
, 'ui.grid.selection'
, 'ui.grid.exporter'
, 'ngLoadingSpinner'
, 'confirm'
, 'transformRequestService'
]);


//injecting in controller
    angular.module('confirm', [])

    .controller('confirmController', ['$scope', '$http', '$timeout', '$uibModal', '$log', '$state', '$filter', '$crypthmac', '$rootScope', '$sce', 'transformRequestAsFormAsPost ', function ($scope, $http, $timeout, $uibModal, $log, $state, $filter, $crypthmac, $rootScope, $sce, transformRequestAsFormAsPost) {
------

}
Posted
Updated 20-Sep-16 3:48am
v2

1 solution

I haven't run your code but I think it is because you are re-declaring your angular module multiple times with different names.

var routerApp = angular.module('myApp', ['ui.router'
, 'vendorLoginModule'
, 'ui.bootstrap'
, 'createUser'
, 'ui.grid'
, 'ui.grid.selection'
, 'ui.grid.exporter'
, 'ngLoadingSpinner'
, 'confirm'
, 'transformRequestService'
]);


So the factory portion would become

routerApp.factory('transformRequestAsFormAsPost', ["$http" , function($http){ }]);


And then your controller would then look like

routerApp.controller("confirmController",
['$scope', '$http', '$timeout', '$uibModal', '$log', '$state', '$filter', '$crypthmac', '$rootScope', '$sce', 'transformRequestAsFormAsPost ', function ($scope, $http, $timeout, $uibModal, $log, $state, $filter, $crypthmac, $rootScope, $sce, transformRequestAsFormAsPost) { }]';


By redeclaring your angular.module and not injecting it here

angular.module('confirm', [ // should inject it here])
 
    .controller('confirmController',


Is why your app is saying unknown provider.

I'm learning angular myself but the route i laid out works for me when implementing factories like this.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900