将变量传递到工厂

Passing in variables to factory?

本文关键字:工厂 变量      更新时间:2023-09-26

我的工厂是一个封装的$resource对象,它需要有一个自定义的头(用于http身份验证)。我不知道如何传入数据以放入标题

app.factory('SomeFactory',['$resource', function($resource){
    return $resource('https://third.party/:token',{token: access_token},{
    get:{
        method:'GET',
        header:{
        'some varialbe': my_var //I want to be able to pass in this var
     }
    }
    });
}])
app.factory('SomeFactory', ['$resource', function($resource){
  return function(access_token,my_var){ // input parameters here
    return $resource('http://third.party/:token',{
      token: access_token
    },{
      get: {
        method: 'GET',
        header: {
          'some variable': my_var
        }
      }
    });
  };
})

或者类似的东西。

app.controller('SomeController', ['SomeFactory', function(someFactory){
  /* ... */
  $scope.myModel.result = someFactory('accesstoken','myvar')
  /* ... */
});