How pass variable from controller to service


#1

i have a var in controller and i need it in service to send in a $http service


#2

pass it as a parameter to a service function, which sends the http request?


#3

Yes, I think the bengtler’s suggestion is the best, but you can use this variable as global using, $rootScope.


#4

Yeah, but i think you should try to avoid using rootScope in services/factories/providers.
The scope/rootScope is introduced for the databinding between controller and view or model view - view model.

greets, bengtler


#5

You can use something like this:

var URL = "http://localhost:8080/arm-web/rest/mobile";

var mod_rest = angular.module('api.rest', []);

mod_rest.factory('ApiRest', function($http){

    function post(service, parameter, callback){
		$http.post(URL + service, parameter)
			.success(function(data, status, headers, config) { 
				setTimeout(function(){
        			callback(data, status, headers, config);
    			}, 1)
			})
			.error(function(data, status, headers, config) { 
				setTimeout(function(){
        			callback(data, status, headers, config);
    			}, 1)
		});
    }

    function get(service, callback) { 

		$http.get(URL + service)
			.success(function(data, status, headers, config) { 
				setTimeout(function(){
        			callback(data, status, headers, config);
    			}, 1)
			})
			.error(function(data, status, headers, config) { 
				setTimeout(function(){
        			callback(data, status, headers, config);
    			}, 1)
		});

    }   

    return {
    	post: post,
    	get: get
    };
    
});