簡體   English   中英

AngularJS無法正確處理JSON

[英]AngularJS not processing JSON correctly

我有一個使用AngularJS作為視圖的Play框架項目。 負責查詢數據的控制器發出2個請求,每個請求返回一個JSON塊。 第一個可以正常工作,並且顯示正確。 Angular破壞了第二個數據(下面的示例)。

JSON會在呈現之前正確創建,如應用程序日志中所示。 這是正確的JSON(摘自Play Framework路由方法的日志):

{"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()執行Play Framework路由的方法。

路由配置:

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":[]}

我設法找到了問題,應該使用$scope.currentService = Service.get(); 而不是$scope.currentService = Service.query();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM