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

Popular posts from this blog

SVG stroke-linecap doesn't work for circles in Firefox? -

routes - Laravel 4 Wildcard Routing to Different Controllers -

cross browser - XSLT namespace-alias Not Working in Firefox or Chrome -