1

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

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

nice solution. I would have added a promise based solution instead of this callback (but both works, it's just a matter of preference and this perfectly answer the question)
0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.