سؤال

I'm facing the problem on passing the argument from controller to service module

I don't know where I have pass the arguments in controller module and how to get the value in service module. here is my code,

Controller module

 var user = $scope.username;
 var pass = $scope.password;

  *// how to pass the username and password here*
  checkStatus.query(function(response, headers) {

            alert(headers('X-Internal-Auth-Toketn'));

  });

Service module

UIAppResource.factory('checkStatus', function($resource) {
    var auth = Base64.encode("abcd:abcd");  // *I need username and password here* 

    return $resource(baseURL + "status", {},
        {
            'query': {
                method: 'GET',
                headers: {
                    'Accept':'application/json',
                    'Content-Type':'application/json',
                    'Authorization': 'Basic '+ auth,
                    'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept'
               },
                isArray: false
            }
        }
    )
});

I'm new for angularjs, please help me to solve this problem

Thanks in advance

هل كانت مفيدة؟

المحلول

Rather than return $resource, return an object that contains functions that use the $resource internally. Eg:

Controller:

var user = $scope.username;
var pass = $scope.password;
checkStatus.customQuery(user, pass, function(response, headers) {
    alert(headers('X-Internal-Auth-Toketn'));
});

Service:

UIAppResource.factory('checkStatus', function($resource) {
    return {
        customQuery: function(user, pass, callback) {
            var auth = Base64.encode(user + ':' + pass); 
            var myRes = $resource(baseURL + "status", {}, {
                'query': {
                    method: 'GET',
                    headers: {
                       'Accept':'application/json',
                       'Content-Type':'application/json',
                       'Authorization': 'Basic '+ auth,
                       'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept'
                    },
                    isArray: false
                }
            });

            myRes.query(callback);
        }
    };
});

Obviously this will create a new $resource for every customQuery() call, so it is most likely better to store the created $resource inside the service once. To do this I would create an initialiser function which the username/password can be passed to.

نصائح أخرى

You should restructure your factory definition that makes easier for your to pass parameters. CheckStatus should be a method on the object returned from factory The definition should look something like

UIAppResource.factory('authService', function($resource) {

var service={};
service.checkStatus=function(userName,passWord) {
   var auth = Base64.encode("abcd:abcd");  // *I need username and password here*         

   return $resource(baseURL + "status", {},
   {
        'query': {
            method: 'GET',
            headers: {
                'Accept':'application/json',
                'Content-Type':'application/json',
                'Authorization': 'Basic '+ auth,
                'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept'
           },
            isArray: false
        }
    }
    )    
}
return service;

});

In you controller then you call

authService.checkStatus($scope.userName,$scope.password);

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top