how to set custom headers with a $resource action?

前端 未结 5 1604
谎友^
谎友^ 2020-12-03 04:37

with $http, we can do this:

var config = { headers: { \'something\': \'anything\' } };          
$http.get(\'url/to/json\', config)
    .success(function()          


        
5条回答
  •  渐次进展
    2020-12-03 04:55

    The headers object inside a resource action supports both static values for its fields, but also dynamic values returned from a function.

    $resource('url/to/json', {}, {
            get: {
                method: 'GET',
                headers: { 
                   'header_static': 'static_value',
                   'header_dynamic': dynamicHeaderVal
                }
            }
    });
    
    function dynamicHeaderVal(requestConfig){
         // this function will be called every time the "get" action gets called
         // the result will be used as value for the header item
         // if it doesn't return a value, the key will not be present in the header
    }
    

提交回复
热议问题