angularJS动态追加参数$resource
angularJS appending parameter dynamically $resource
我试图在$resources url中设置一个动态参数。
我发现这个帖子很有帮助,但遗憾的是它没有解决我的问题。$resource函数中仍然没有填写cidmessageApp.factory('conversationService', function($http) {
var data = {cid: ''};
$http({method: "GET", url: '/myApp/resources/conversationwizard'}, {cache: true})
.success(function(d) {
data.cid = d;
})
.error(function(data) {
alert("Could not initiate conversation" + data);
});
return data;
});
messageApp.factory('messagesService', function($resource) {
return $resource('/myApp/resources/message/all', {}, {
query: {method: 'GET', isArray: true}
});
});
messageApp.factory('messageEventPoller', function($http, $timeout, conversationService) {
var data = {data: ''};
var poller = function() {
$http.get('/myApp/msgnotification?cid=' + conversationService.cid).then(function(r) {
data.data = r.data;
$timeout(poller, 1);
});
};
poller();
return data;
});
messageApp.factory('createMessageService', function($resource, conversationService) {
return $resource('/myApp/resources/message?cid=:cid', {cid: conversationService.cid}, {
create: {method: 'POST'}
});
});
messageApp.factory('messageService', function($resource, conversationService) {
return $resource('/myApp/resources/message/:id?cid=:cid', {cid: conversationService.cid}, {
show: {method: 'GET'},
update: {method: 'PUT', params: {id: '@id'}},
delete: {method: 'DELETE', params: {id: '@id'}}
});
});
您遇到的问题是conversationService.cid
是使用单独的异步 ajax请求从服务器获取的。
配置:$resource('/myApp/resources/message?cid=:cid', {cid: conversationService.cid}
时,响应尚未到达, conversationService.cid
仍为空
不幸的是,没有简单的解决方案。这里我提出一些建议:
1)使用同步ajax从jQuery与async : false
:(不太好,因为它阻塞了浏览器线程)
messageApp.factory('conversationService', function($http) {
var data = {cid: ''};
$.ajax({
url: '/myApp/resources/conversationwizard',
async : false
})
.done(function(d) {
data.cid = d;
})
.fail(function(){
alert("Could not initiate conversation" + data);
});
return data;
});
2)加载你的'/myApp/resources/conversationwizard'
到一个全局对象之前,引导angular(使用jQuery ajax)。在conversationService
中,你可以分配data.cid = yourglobal
;
messageApp.factory('conversationService', function($http,$q) {
var deferred = $q.defer();
var data = {cid: ''};
$http({method: "GET", url: '/myApp/resources/conversationwizard'}, {cache: true})
.success(function(d) {
data.cid = d;
deferred.resolve(data);
})
.error(function(data) {
alert("Could not initiate conversation" + data);
});
return deferred.promise;
});
messageApp.factory('createMessageService', function($resource, conversationService) {
var deferred = $q.defer();
conversationService.then(function (data){
var createMessageService = $resource('/myApp/resources/message?cid=:cid',
{cid: data.cid},
{create: {method: 'POST'}
});
deferred.resolve(createMessageService);
});
return deferred.promise;
});
但是这会增加使用资源的代码的复杂性。例如
messageApp.controller("testCtrl",function (createMessageService){
createMessageService.then(function(service){
//use your service here
});
});
相关文章:
- 使用Angular Resource查询具有字符串参数的REST API
- angular$resource delete-传递id参数
- 添加新参数到$resource后使用ngChange,Angular
- 尝试从模板将参数发送到$resource工厂
- 如何在 angularjs 中发送带有 $resource 请求的 URL 参数
- 如何将控制器变量作为 AngularJS $resource请求的参数传递
- 将参数传递给$resource(使用 @)
- 如何使用AngularJS$resource.query传递参数
- forEach AngularJS中的$resource函数参数
- $resource's url参数的冒号模板语法是遵循标准的,还是angularjs特有的?
- Angular $resource模板没有正确地使用参数
- angularJS动态追加参数$resource
- Angular $resource如何在URL中不发送参数
- 如何使用Angular $resource传递查询参数数组
- 如何向AngularJS $resource传递参数
- Mean Stack——Angular将参数从控制器传递到$resource对象
- Angular $resource POST请求查询参数未被AWS API网关检测到
- 将自定义参数传递给$resource和transformResponse
- AngularJS $resource GET中的多个参数
- Angularjs:如何传递";字符转换为$resource的参数