如何在 angular $resource 中传递动态参数
How to pass dynamic param in angular $resource
我想在 $resource 中传递默认参数的动态值。请查看Plunker。在这里,当我在工厂中传递默认参数时
app.factory('myFactory', ["$resource", function($resource) {
return $resource('http://google.com', {timeStamp : (new Date()).getTime()}, {
query : {
method : 'GET',
isArray : true,
cache : false
}
})
}]);
我希望默认参数时间戳是动态的。
当前,当我点击 刷新 按钮时,参数值始终相同 (在控制台中检查)
I need this functionality because IE make cache on GET method and I don't want cache data
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
}
})
}]);
函数内部的代码只有在函数被调用时才会执行。然而,在一个对象的声明中,所有的声明都是在对象被创建时执行的。你可能也有过这个。
app.factory('myFactory', ["$resource", function($resource) {
return $resource('http://google.com', {timestamp: 1433865286924}, {
query : {
method : 'GET',
isArray : true,
cache : false
}
})
}]);
这应该对你有帮助。
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
}
})
}]);
每次调用 myFactory.query()
方法时,带有当前时间戳值的查询字符串 ts
将出现在 url.
中
Lekhnath 所说的正确答案是
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
}
})
}]);
或者如果我们想传入默认参数部分,可以按
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
}
})
}]);
我想在 $resource 中传递默认参数的动态值。请查看Plunker。在这里,当我在工厂中传递默认参数时
app.factory('myFactory', ["$resource", function($resource) {
return $resource('http://google.com', {timeStamp : (new Date()).getTime()}, {
query : {
method : 'GET',
isArray : true,
cache : false
}
})
}]);
我希望默认参数时间戳是动态的。 当前,当我点击 刷新 按钮时,参数值始终相同 (在控制台中检查)
I need this functionality because IE make cache on GET method and I don't want cache data
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
}
})
}]);
函数内部的代码只有在函数被调用时才会执行。然而,在一个对象的声明中,所有的声明都是在对象被创建时执行的。你可能也有过这个。
app.factory('myFactory', ["$resource", function($resource) {
return $resource('http://google.com', {timestamp: 1433865286924}, {
query : {
method : 'GET',
isArray : true,
cache : false
}
})
}]);
这应该对你有帮助。
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
}
})
}]);
每次调用 myFactory.query()
方法时,带有当前时间戳值的查询字符串 ts
将出现在 url.
Lekhnath 所说的正确答案是
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
}
})
}]);
或者如果我们想传入默认参数部分,可以按
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
}
})
}]);