Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to pass dynamic value of default param in $resource. Please look into the Plunker. Here when I had passed default param in factory as

app.factory('myFactory', ["$resource", function($resource) {
    return $resource('http://google.com', {timeStamp : (new Date()).getTime()}, {
        query : {
            method : 'GET',
            isArray : true,
            cache : false
        }
    })
}]);

I want default param timeStamp to be dynamic. currently when i click on Refresh Button the param value goes always same (Check in console)

I need this functionality because IE make cache on GET method and I don't want cache data

share|improve this question

3 Answers 3

up vote 1 down vote accepted

This should help you.

app.factory('myFactory', ["$resource", function($resource) {
    return $resource('http://google.com', {}, {
        query : {
            method : 'GET',
            isArray : true,
            params : { ts : Date.now } // <-- new timestamp will be genrated and assigned to ts
        }
    })
}]);

each time you invoke myFactory.query() method a querystring ts with current timestamp value will appear in url.

share|improve this answer
    
it still does not give the dynamic values of ts. once the value of ts is set then if you click button multiple times same value goes. –  Amit Mourya 2 hours ago
    
Oh yes, I made mistake there by calling Date.now() directly. You can try this edited one. –  Lekhnath 2 hours ago
var app = angular.module('plunker', ['ngResource']);

app.controller('MainCtrl', function($scope,myFactory) {
  $scope.refresh = function(){
    myFactory.query({timeStamp : (new Date()).getTime()}).$promise.then(function(){})
  }
});
app.factory('myFactory', ["$resource", function($resource) {
    return $resource('http://google.com', {}, {
        query : {
            method : 'GET',
            isArray : true,
            cache : false
        }
    })
}]);

Code inside of a function is not executed until the function is called. However within a declaration of an object, all of it is executed as the object's being created. You may as well have of had this.

app.factory('myFactory', ["$resource", function($resource) {
    return $resource('http://google.com', {timestamp: 1433865286924}, {
        query : {
            method : 'GET',
            isArray : true,
            cache : false
        }
    })
}]);
share|improve this answer
    
Hi @Matt I dont want to pass the parameter while calling the factory function. I want it to be done with default params –  Amit Mourya 4 hours ago

The correct answer as said by Lekhnath is

app.factory('myFactory', ["$resource", function($resource) {
    return $resource('http://google.com', {}, {
        query : {
            method : 'GET',
            isArray : true,
            params : { ts : Date.now } // <-- new timestamp will be genrated and assigned to ts
        }
    })
}]);

or if we want to pass in default param section it can be done as

app.factory('myFactory', ["$resource", function($resource) {
    return $resource('http://google.com', { ts : Date.now }, { // <-- new timestamp will be genrated and assigned to ts
        query : {
            method : 'GET',
            isArray : true
        }
    })
}]);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.