如何使用angularjs显示从控制器返回的json数据

时间:2016-02-25 02:06:45

标签: angularjs angularjs-ng-repeat angular-http

我想使用angularjs显示从控制器返回的json编码数据到我的视图。

<div ng-app="memberApp" ng-controller="memberCtrl">
    <input type="text" ng-model="search" />Search
    <table class="table table-bordered">
        <thead>
            <tr>
                <th ng-click="orderByMe('name')">Name</th>
                <th ng-click="orderByMe('country')">Country</th>
            </tr>
        </thead>
            <tr ng-repeat="member in members track by $index | toArray:false | filter:search ">
                <td>{{member.FirstName + " " + member.LastName}}</td>
                <td>{{member.Country}}</td>
            </tr>
        <tbody>
        </tbody>
    </table>
</div>

在我的脚本中我使用了$ http服务。请帮忙!

angular.module('memberApp', []).controller('memberCtrl', function($scope, $http) {
    $scope.members = [];
    var httpRequest = $http({
        method: 'POST',
        url: '<?php echo site_url('member/getMemberList'); ?>',
        data: {},
        dataType: 'json'
    }).success(function(data){
        $scope.members = data;
    });
});

数据就是这样的

{"members":[{"FirstName":"Matthew","LastName":"dennis","City":"Mississauga","Country":"Canada"}]

2 个答案:

答案 0 :(得分:1)

您想要的数组是data.members

最简单的是将php更改为仅返回该数组而没有额外的对象和属性members或更改控制器

$scope.members = data;

$scope.members = data.members;

答案 1 :(得分:0)

我明白了。而不是数据,它是data.members。

angular.module('memberApp', []).controller('memberCtrl', function($scope, $http) {
    $scope.members = [];
    var httpRequest = $http({
        method: 'POST',
        url: '<?php echo site_url('member/getMemberList'); ?>',
        data: {},
        dataType: 'json'
    }).success(function(data){
         $scope.members = data.members;
    });
});