angular provider get method has no reference of set variable from config block

37 views Asked by At

I have below serviceURL Angular Provider in my project.

app.provider("serviceURL", function () {

var cartServiceBaseSite = '';
var domainName = '';

var GetRatingURL = domainName + '/comments.getComments?format=jsonp&callback=JSON_CALLBACK&data={0}&&order={1}='                    

return {
        setCartServiceBaseSite: function(value){
            cartServiceBaseSite = value;
        },
        setDomainName: function(value){
            domainName = value;
        },
        $get: function () {
          return {
              GetServiceURL: function(serviceConstantURLName, paramsArray){
                var requestURL = eval(serviceConstantURLName);

                 for(var i = 1; i <= paramsArray.length; i++) {
                    requestURL = requestURL.replace(new RegExp('\\{' + (i-1) + '\\}', 'gi'), paramsArray[i-1]);
                }

                 return requestURL;
              }
          }
        }
    };
}); 

I am setting cartServicebaseSite & domainName variable which is as good as constants from angular config block.

var app = angular.module('myModule', []);

app.config(['serviceURLProvider', function (serviceURLProvider) {

  serviceURLProvider.setCartServiceBaseSite('US_BASESITE');
  serviceURLProvider.setGigyaDomainName('http://somerandom.com');

}]);

I am injecting this serviceURL provider in a controller and getting a URL back after proper setup.

Controller code:

myModule.controller('T', ['serviceURL', function (serviceURL){

  var paramsArray = [];
  paramsArray[0] = 'dataValue';
  paramsArray[1] = 'orderValue';

  serviceURL.GetServiceURL('GetRatingURL', paramsArray);

}]);

in Provider block when I call GEtServiceURL method I am passing parameters as 'GetRatingURL' which is nothing but a variable in provider block.

I am using eval expression to evaluate that, where I am not getting domainName value which we have set it from setDomainName.

current output - '/comments.getComments?format=jsonp&callback=JSON_CALLBACK&data=dataValue&order=orderValue'

expected output - 'http://somerandom.com/comments.getComments?format=jsonp&callback=JSON_CALLBACK&data=dataValue&order=orderValue'

issue is domainName is not getting set in GetRatingURL.

can anyone help on this or what should be the approach to handle this situation.

1

There are 1 answers

0
georgeawg On

Add a console.log to see what is happening:

return {
        setCartServiceBaseSite: function(value){
            cartServiceBaseSite = value;
            //ADD console.log
            console.log("Setting BaseSite "+value);
            console.log("GetRatingURL= "+ GetRatingURL);
        },

The results should tell you what you need to know.