angularJS动态追加参数$resource

angularJS appending parameter dynamically $resource

本文关键字:resource 参数 追加 动态 angularJS      更新时间:2023-09-26

我试图在$resources url中设置一个动态参数。

我发现这个帖子很有帮助,但遗憾的是它没有解决我的问题。$resource函数中仍然没有填写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'}}
    });
});

您遇到的问题是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;

3)使用angular的承诺:
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
     });
});