rajagopalx
rajagopalx

Reputation: 3104

In Angular JS, How to pass value to provider?

For some reasons, I want to pass masterapp.value ('mastervalue' , 'XYZ') to provider. How to do that ? Below approach throw error.

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

masterapp.value ('mastervalue' , 'XYZ');

masterapp.provider('masterprovider', ['mastervalue', function(mastervalue) {

    this.myFn = function() {
        return mastervalue;
    };

 }]);

Upvotes: 0

Views: 93

Answers (2)

S.Klechkovski
S.Klechkovski

Reputation: 4045

You need to make it constant if that is fine with you (cannot be changed). So the updated code will be:

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

masterapp.constant('mastervalue' , 'XYZ');

masterapp.provider('masterprovider', ['mastervalue', function(mastervalue) {

    this.myFn = function() {
        return mastervalue;
    };
}]);

Upvotes: 1

Mifune
Mifune

Reputation: 128

I'm not sure if .value can be returned like that. Try assigning it first within scope and then using that value to create the function.

this.masterValue = mastervalue;
this.myFun = function(){
      return this.masterValue
}

Upvotes: 1

Related Questions