javascript - $http is undefined in service despite injecting it -
i trying create service provide js api integration between angular app , server talks to. however, whenever try use $http in service undefined.
the service instantiated $scope.broker in main controller:
function mainctrl($scope, $http) { $scope.broker = new broker(); ... angular .module('inspinia') .controller('mainctrl', ['$scope','$http',mainctrl])
the service used in controller attached same module:
function locations($scope, $http){ console.log($scope.broker.ping); $scope.broker.ping(); angular.module('sentry.locations', []) .controller('locations', ['$scope','$http', locations]);
service definition:
function broker($http) { var baseurl = 'localhost:8000'; var broker = {}; broker.ping = function() { console.log(this); $http({ method: 'get', url: baseurl + '/hello' }) }; console.log($http); return broker; }; angular.module('inspinia') .factory('broker', ['$http', broker]);
console.log($scope.broker.ping) correctly prints ping function.
console.log($http) prints undefined
thanks in advance help.
edit: added mainctrl registration after elipses
$scope.broker = new broker();
your broker new
function in mainctrl
,not injected angular service,so $http
undefined.
Comments
Post a Comment