0

I am trying to set a header value based on the value in my controller. I have the header sending when I am hard coding it as shown below but how can I pass the value from the controller into the ngResource get request,

eg I want the value of anything in the headers to be the value from my controller.

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

app.factory('UserService', function ($resource) {
    return $resource('http://jsonplaceholder.typicode.com/users/:user',{user: "@user"},{
    get: {
            method: 'GET',
            headers: { 'something': 'anything' }
        }
    });
});

app.controller('TestCtrl', function($scope, $resource, UserService) {
  $scope.test = "text";
   UserService.get({
      user: 2
   }, function(data) {
     $scope.id = data.id;
    }, function(err) {
      console.log(err);
    });
});
<!doctype html>
<html ng-app="app">
  <head>
    <meta charset="utf-8">
    <title ng-bind="title" ng-cloak>Restaurant</title>
  </head>

  <body ng-controller="TestCtrl">
id - {{id}}
    <!-- Don't forget to load angularjs AND angular-resource.js --> 
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular-resource.js"></script>
    <!--Controllers-->
  </body>
</html>
ak85
  • 4,154
  • 18
  • 68
  • 113
  • 1
    I think this post will help You: [http://stackoverflow.com/questions/20566593/dynamic-resource-headers](http://stackoverflow.com/questions/20566593/dynamic-resource-headers) – Bata Dec 10 '15 at 23:40
  • @Bata thanks for that, this helped me out. – ak85 Dec 11 '15 at 12:05

0 Answers0