2014-09-29 51 views
0

我有一个使用AngularJS进行视图的Play框架项目。负责查询数据的控制器发出2个请求,每个请求返回一个JSON块。第一个工作正常,并显示正确。第二个数据几乎被Angular摧毁了(下面的例子)。AngularJS不能正确处理JSON

JSON在呈现前正确创建,因为它通过应用程序的日志显示。 这是正确的JSON(从Play框架路由方法的记录所采取的):

{"id":5,"name":"auditoria","url":null,"version":1,"methods":[]} 

这是AngularJS如何打印它。它标记化:

[{},{"0":"a","1":"u","2":"d","3":"i","4":"t","5":"o","6":"r","7":"i","8":"a"},{},{},{"length":0}] 

而这里的控制器:

app.controller("ViewCtrl", [ "$scope", "$resource", "$routeParams", "apiUrl", 
     function($scope, $resource, $routeParams, apiUrl) { 

      var ServiceList = $resource(apiUrl + "/services"); 
      $scope.services = ServiceList.query(); //JSON is displayed properly 

      if ($routeParams.id) { 

       jsonServicoQuery = apiUrl + "/services/" + $routeParams.id 
       var Service = $resource(jsonServicoQuery); 
       $scope.currentService = Service.query(); //JSON is butchered 
      } 
     } ]); 

这里的HTML:

<div class="row"> 
    <div class="col-md-3"> 
     <div class="bs-sidebar hidden-print" role="complementary"> 
      <ul class="nav bs-sidenav"> 
       <li><a href="/create"><i class="fa fa-plus"></i></a></li> 
       <li ng-repeat="s in services| orderBy:'name'"><a 
        href="/view/{{s.id}}">{{s.nome}}</a>  
        <ul> 
         <li ng-repeat="m in s.methods| orderBy:'name'">{{m.name}} 
          [{{m.id}}]</li> 
        </ul></li> 
      </ul> 
     </div> 
    </div> 

    <div class="col-md-9" role="main"> 
     <div class="bs-docs-section"> 
      <div class="page-header"> 
<!-- displaying the whole currentService JSON for debugging purposes --> 
      {{currentService}} 
      </div> 
     </div> 
    </div> 
</div> 

任何人有什么我做错了什么线索?


更新: Service.query()执行由游戏框架路由的方法。

路由配置:

GET  /api/services/:id    controllers.Services.show(id: String) 

而且controllers.Services.show(id: String)实现:

public static Result show(String id) { 
    Service s = Service.findById(id); 

//JSON displayed here is correct, log below 
    Logger.info("At Services show, json " + Json.toJson(s)); 

    return ok(Json.toJson(s)); 
} 

登录:

[info] application - At Services show, json {"id":5,"name":"auditoria","url":null,"version":1,"methods":[]} 
+0

看起来你的服务有问题。你可以发布你的Service.query()的内容吗? – 2014-09-29 16:52:00

回答

0

我设法找到了问题,我应该用$scope.currentService = Service.get();代替$scope.currentService = Service.query();