angularjs - Unable to inject $http into a factory -



angularjs - Unable to inject $http into a factory -

i'm trying inject $http factory i'm getting error:

error: unknown provider: $httpprovider <- $http <- utilservice

here factory code:

var util = angular.module('util', []) .factory('utilservice', ['$http', function($http) { homecoming { translate: function(objects, prop) { var keys = objects.map(function(o) {return o[prop];}); $http({method: 'post', url: 'http://localhost:8080/rest/messages', data: keys}). success(function(data, status, headers, config) { homecoming objects.map(function(o, rank) {return {'value': data[rank], 'object': o}}); }); } }; }]);

the code of controller utilize service:

var app = angular.module('recordcontroller', ['ui.bootstrap','dialogs', 'util']); function records($scope, $http, $rootscope, $timeout, $dialogs) { var utilservice = angular.injector(['util']).get('utilservice'); $scope.showform = false; $scope.states= []; $scope.toggleform = function() { $scope.showform = !$scope.showform; if($scope.showform) { if(!$scope.states.length) { $http({method: 'get', url: 'http://localhost:8080/rest/records/state'}). success(function(data, status, headers, config) { $scope.states = utilservice.translate(data, 'state'); }); } } }; ... };

and html:

<div ng-app='recordcontroller'> <div ng-controller='records' > ... </div> </div>

is wrong it? have been searching insight in stackoverflow , google couldn't find hint what's wrong.

i haven't tested of believe problem can fixed in multiple ways:

1

use angular.module('util', ['ng']) instead of angular.module('util', []).

2

use app.get('utilservice') instead of angular.injector(['util']).get('utilservice').

3

get rid of var utilservice = angular.injector(['util']).get('utilservice'); , add together utilservice lastly parameter of records function. i'm not sure works don't utilize form of declaration.

if #3 works, utilize that, if not utilize #2. utilize #1 if have to.

also, did not post part of code records used, create sure using array notation dependencies otherwise you'll have issues when mangling code uglifyjs or similar too.

angularjs

Comments