需要帮助将数据 JSON 提取到范围 AngularJS

Need help fetching data JSON to scope AngularJS

我是 AngularJS 的新手,现在正在尝试学习它。这次我正在学习如何使用 codeigniter 和 angular 以及 RESTful lib.

创建单页应用程序

我有 JSON 这样的 :

[{"id":"25","title":"abc","description":"sss"},{"id":"26","title":"sd","description":"sdsd"}]

无论我做什么,我都无法通过 ng-repeat 将其提取到 $scope。这是我的职能:

$http.get('Api/items').then(function(response)
        {       
            $scope.items = response;
            console.log(response);
        });

这是我的 table 观点:

<table class="table table-bordered pagin-table">
<thead>
    <tr>
        <th>No</th>
        <th>Title</th>
        <th>Description</th>
        <th width="220px">Action</th>
    </tr>
</thead>
<tbody>
    <tr ng-repeat="x in items">
        <td>{{ $index + 1 }}</td>
        <td>{{ x.title }}</td>
        <td>{{ x.description }}</td>
        <td>
            <button data-toggle="modal" ng-click="edit(x.id)" data-target="#edit-data" class="btn btn-primary">Edit</button>
            <button ng-click="remove(x,$index)" class="btn btn-danger">Delete</button>
        </td>
    </tr>
</tbody>

这是所有执行时发生的情况:

$scope 根本没有获取任何数组。奇怪的是 <tr> 重复了 5 次而没有任何值。但是当我调用 array[0] 时,$scope 可以获取它:

<table class="table table-bordered pagin-table">
<thead>
    <tr>
        <th>No</th>
        <th>Title</th>
        <th>Description</th>
        <th width="220px">Action</th>
    </tr>
</thead>
<tbody>
    <tr ng-repeat="x in items">
        <td>{{ $index + 1 }}</td>
        <td>{{ x[0].title }}</td>
        <td>{{ x[0].description }}</td>
        <td>
            <button data-toggle="modal" ng-click="edit(x.id)" data-target="#edit-data" class="btn btn-primary">Edit</button>
            <button ng-click="remove(x,$index)" class="btn btn-danger">Delete</button>
        </td>
    </tr>
</tbody>

我哪里错了??有人可以帮帮我吗??

这是因为承诺从服务器返回原始 HTTP 响应而不是解析的结果数据。你应该试试这个:

$http.get('Api/items').then(function(response)
        {       
            $scope.items = response.data;
            console.log(response);
        });

@digit 已经在你的 $hhtp 电话中指出了这个问题。现在为此 JSON:

 [{"id":"25","title":"abc","description":"sss"},{"id":"26","title":"sd","description":"sdsd"}]

。 无需在 ng-repeat 中提供索引号。 //{{ x[0].title }}

<tr ng-repeat="x in items">
        <td>{{ $index + 1 }}</td>
        <td>{{ x.title }}</td>
        <td>{{ x.description }}</td>
        <td>
            <button data-toggle="modal" ng-click="edit(x.id)" data-target="#edit-data" class="btn btn-primary">Edit</button>
            <button ng-click="remove(x,$index)" class="btn btn-danger">Delete</button>
        </td>
    </tr>