1
我想在$ resources URLs中设置一个动态参数。
我发现this有用的文章,但遗憾的是它并没有解决我的问题。 $资源函数中仍未填充cid。
messageApp.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'}}
});
});
任何指针将不胜感激。
好的,理解。不过,我不知道如何解决这个问题,我最近才刚开始使用Javascript和angularJS。你会如何解决这个问题? – dngfng
也不应该缓存:真正解决这个问题? – dngfng
@dngfng:恐怕我们没有解决这个问题的办法,因为我们无法将已解决的promise作为依赖项注入到'factory'中。解决方法可能是:在引导角度之前(使用jQuery ajax)将''/ myApp/resources/conversationwizard''加载到全局对象中。在你的'conversationService'中,你可以分配'data.cid = yourglobal;'。 –