Java Angular JS-如何在Angular控制器中获取spring返回的映射值?

Java Angular JS-如何在Angular控制器中获取spring返回的映射值?,java,angularjs,spring,Java,Angularjs,Spring,我是angular的新手,有人能告诉我如何在angular的控制器中检索spring返回的映射值吗? 以下是我的代码片段: app.js // Service ----------------------------------------------------------------- myApp.service('FolderService', function ($log, $resource, $http) { return { onlineView: f

我是angular的新手,有人能告诉我如何在angular的控制器中检索spring返回的映射值吗? 以下是我的代码片段:

app.js

   // Service -----------------------------------------------------------------
myApp.service('FolderService', function ($log, $resource, $http) {
    return {
        onlineView: function(docId) {
            var viwerResource = $resource('processOnlineView', {}, {
                get: {method: 'POST', params: {'docId' : docId}}
            });
            return viwerResource.get();
        }       
    }
})

// Controller -----------------------------------------------------------------
.controller('FolderController', function ($scope, $log, FolderService) {        
    //click online view         
    $scope.view = function(doc) { 
        var rtnMap = FolderService.onlineView(doc.cmObjectId);
        console.log('rtnMap: ' + rtnMap );  
        // it shows rtnMap: [object Object]
        var key = 'response'; 
        var value = rtnMap[key];
        console.log('value: ' + value ); 
       // I want to get map value, but it shows undefined
       // I except get "d:/tomcat/bin/hello.swf" here
        $scope.rtnFileName = rtnMap;
    }        
}); 
我的spring控制器java代码

@RequestMapping(value = "/processOnlineView", method = RequestMethod.POST)
public @ResponseBody Map<String, String> processOnlineView(@RequestParam(value = "docId") String docId) {
    String resultDocName = "";
    try {
        // get File by docId
        File file = queryFile(docId);    

       // set resultDocName value
        resultDocName = file.getAbsolutePath(); // real file path, like: d:/tomcat/bin/hello.swf
    } catch (Exception e) {
        e.printStackTrace();
        }
    return Collections.singletonMap("response", resultDocName);
    }    
但如何在角度控制器中直接获取贴图值?
如有任何建议,将不胜感激

使用服务。例如:

var app = angular.module('myApp', [])
app.service('sharedProperties', function () {
    var mapCoord= 'Test';

    return {
        getProperty: function () {
            return mapCoord;
        },
        setProperty: function(value) {
            mapCoord= value;
        }
    };
});
在主控制器内部

app.controller('Main', function($scope, sharedProperties) {
    $scope.mapCoord= sharedProperties.setProperty("Main"); 
});
app.controller('Map', function($scope, sharedProperties) {
    $scope.mapCoord= sharedProperties.getProperty(); 
});
在地图控制器内

app.controller('Main', function($scope, sharedProperties) {
    $scope.mapCoord= sharedProperties.setProperty("Main"); 
});
app.controller('Map', function($scope, sharedProperties) {
    $scope.mapCoord= sharedProperties.getProperty(); 
});
问题解决了。 首先,使用$http post而不是$resource:

            onlineView: function(docId) {
                $http({
                    method: 'POST',
                    url: urlBase + '/processOnlineView',
                    params: {
                        docId: docId
                    }
                })
                .success(function(data, status, headers, config) {
                    console.log('success data: ' + data); // result: success data: [object Object]                        
                    for (key in data){  
                        console.log('>> data key: ' + key );           
                        console.log('>> data value: ' + data[key] ); 
                    }            
                    var resultDocName = data['response'];    
                    console.log('resultDocName: ' + resultDocName);        
                    runFlexpaper(resultDocName);
                })
                .error(function(data, status, headers, config) {

                });                              
        }

第二,在“success”块中检索返回的映射,因为$HTTPPOST是异步调用。

您能分享从中得到的结果吗?FolderService.onlineView(doc.cmObjectId)。也就是说,你得到了什么样的回应?json?或者与我们分享一下,当你在调用onlineView()后呈现这个{{rtnFileName.response}}时,你在html中看到了什么,它会返回一个json格式映射[object object],我在我的帖子中添加了更多细节,谢谢!我不知道你想做什么。您的服务“/processOnlineView”似乎只返回一个条目(即“d:/tomcat/bin/hello.swf”),而不是实际的列表/映射。。。你能分享一下你的实际服务电话结果吗?您可以在google chrome的网络选项卡上找到这个。谢谢您的回复,但我需要在spring回调后立即检索地图值,谢谢:)