如何使用Ionic从外部JSON获取数据?

时间:2016-01-27 21:02:27

标签: javascript angularjs json ionic-framework

目前我正在使用Ionic Framework开发应用程序。

我需要使用令牌和参数从外部JSON文件获取数据(1代表高级版,2代表基本版)

网址正确显示数据,但我不明白如何从“pageCtrl”获取数据。

我试试这个http://fdietz.github.io/recipes-with-angular-js/consuming-external-services/requesting-json-data-with-ajax.html

但在应用程序中只能看到3个没有数据的块

我在controller.js上使用此代码

// Controller of docs.
appControllers.controller('docsCtrl', function ($scope, $mdBottomSheet, $mdToast, $mdDialog, $http) {

    $http.get('https://www.domain.com/api/document?__token=[the_token_added_on_code]&__idPortal=1').
    success(function(data, status, headers, config) {
        $scope.docs = data;
    }).
    error(function(data, status, headers, config) {
    // log error
    });

}); // End of docs controller.

在HTML上:

<ion-view view-title="Documentos">

    <!--dashboard section-->
    <ion-content id="docs-content">

        <div class="documentos padding">

            <ion-list>
                <ion-item ng-repeat="doc in docs" class="item-avatar">
                    <h2>{{doc.name}}</h2>
                    <p>{{doc.description}}</p>
                    <p>{{doc.created}}</p>
                </ion-item>
            </ion-list>
        </div>

    </ion-content><!--end dashboard section-->
</ion-view>

最后,URL返回此数据:

{"success":true,"return":{"totalItem":"33","totalPages":7,"pageSize":5,"itens":[{"id_document":"4760","name":"Teste","created":"02\/09\/2015 16:57:00","id_type":"108","type":"Documento Teste","description":"Documento"},{"id_document":"4722","name":"Ata de assembleia 08\/2015","created":"31\/08\/2015 17:32:55","id_type":"3","type":"Ata da assembl\u00e9ia","description":null},{"id_document":"4400","name":"Regimento","created":"04\/08\/2015 16:47:30","id_type":"108","type":"Documento Teste","description":"Regimento interno "},{"id_document":"4261","name":"ATA da AGE em 18\/09","created":"26\/07\/2015 22:22:39","id_type":"3","type":"Ata da assembl\u00e9ia","description":null},{"id_document":"2964","name":"Novo regimento playground","created":"05\/05\/2015 14:30:17","id_type":"91","type":"Regimento Interno","description":"Segue novo regimento playground"}]}}

提前感谢您的支持

1 个答案:

答案 0 :(得分:2)

如果您正确地返回数据,则必须从attr itens

获取项目
 $scope.docs = data.itens;

所以请记录您的数据console.log(data)以找到对象中的属性并导航到它 {status: true, return: { .. itens:[] }}

$scope.docs = data.return.itens