【问题标题】:Generating table from json in AngularJS在AngularJS中从json生成表
【发布时间】:2014-06-01 19:10:02
【问题描述】:

我正在尝试从 AngularJS 中的 json 生成 HTML 表。 我收到格式如下的 JSON:

我获取数据的服务如下所示:

customAPI.getUsers = function() {
            return $http({
                method: 'JSONP',
                url: 'http://arka.foi.hr/WebDiP/2013_projekti/WebDiP2013_069/api/admin/users.php'
            });
        };

该代码的控制器看起来像这样

controller('usersController', function($scope, customAPIservice) {
        $scope.filterName = null;
        $scope.usersList = [];

        /*Search*/
        $scope.searchFilter = function(user) {
            var keyword = new RegExp($scope.filterName, 'i');
            return !$scope.filterName || keyword.test(user.korisnici.korisnik_ime) || keyword.test(user.korisnici.korisnik_prezime);
        };

        customAPIservice.getUsers().success(function(response) {
            $scope.usersList = response.korisnici;
        });
    });

我的观点是这样的:

<input type="text" ng-model="nameFilter" placeholder="Trazi..."/>
<h2 >Korisnici</h2>
<table>
    <thead>
        <tr>
            <th colspan="6">Korisnici sustava</th>
        </tr>

    <th>Surname</th>
</thead>
<tbody>
    <tr ng-repeat="user in usersList| filter: searchFilter">
        <td>{{$index + 1}}</td>


        <td>{{user.korisnik.korisnik_prezime}}</td>
        <td>{{user.korisnik.korisnik_username}}</td>
    </tr>


    <tr  ng-show="usersList == ''">
        <td colspan="5">
            <img src="img/ajax-loader.gif" />
        </td>
    </tr>

</tbody>
</table>

我想我在将数据与视图绑定的地方搞砸了,但我对 Angular 还是很陌生,所以我找不到问题所在。另外我在网上查了一下,找不到任何东西。请帮忙。

【问题讨论】:

  • 在您的控制器中,您有customAPIservice,但您的服务定义为customAPI
  • 是的,我知道,但这只是服务 js 的一部分,所以这些东西可以工作,因为我收到 JSON 响应,用它制作表格是个问题

标签: javascript json angularjs angularjs-scope angularjs-ng-repeat


【解决方案1】:

您没有正确访问数据中的属性。使用:

/*Search*/ $scope.searchFilter = function(user) { var keyword = new RegExp($scope.filterName, 'i'); return !$scope.filterName || keyword.test(user.korisnik.korisnik_ime) || keyword.test(user.korisnik.korisnik_prezime); };

【讨论】:

  • 可能你是对的,但我无法正确测试它,因为我无法生成表格:/
  • 添加 Batarang 以便查看正在加载的数据? chrome.google.com/webstore/detail/angularjs-batarang/…
  • 我做了,它说范围是空的,问题就在这里:customAPIservice.getUsers().success(function(response) { $scope.usersList = response.korisnici; });和 ng 重复。好像我没有向范围添加任何数据。
  • 如果你想创建一个 plunkr 我可以看看
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-10
  • 1970-01-01
相关资源
最近更新 更多