如何使用 angularjs 从控制器显示 json 数据 return

How can I display json data return from the controller using angularjs

我想使用 angularjs 在我的视图中显示来自控制器的 json 编码数据 return。

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

您想要 data.members.

的数组

最简单的方法是将 php 更改为仅 return 没有额外对象的数组 属性 members 或在控制器中更改

$scope.members = data;

$scope.members = data.members;

我明白了。它不是数据,而是 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;
    });
});