質問

I got a problem with angularJS , I got two factories and when I want to call a methode of one factory in the other I get this error : TypeError: Cannot call method 'getValue' of undefined

my two factories :

technoServices.factory('Shared',
    function(){
        var Shared={};
        Shared.setValue=function(header){
                Shared.header = header;
            }
        Shared.getValue=function(){

                return Shared.header;
            }
        return Shared;
        });

technoServices.factory('Config',['$resource','Shared',
  function($rootScope,$resource,token,Shared){

return $resource('../../rest/config',null,{
    get: {
        method:'GET',
        headers:{'X-Token':''+Shared.getValue()},
        isArray:false}
});
}]);

The error is when i do : Shared.getValue()

役に立ちましたか?

解決

In your Config factory definition injection array is not matching the parameters of the function. You declared two elements whereas your function signature expects four arguments so the last two are going to be undefined.

technoServices.factory('Config',['$resource','Shared',
  function($rootScope,$resource,token,Shared){

Something like:

technoServices.factory('Config',['$rootScope', '$resource', 'token', 'Shared',
  function($rootScope,$resource,token,Shared){
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top