在 Angular JS 中显示视图上的数组

Showing array on view in Angular JS

本文关键字:数组 视图 显示 Angular JS      更新时间:2023-09-26

我必须在我用Angular JS制作的表上显示一个JSON数组值。当我加载页面时,调用 Web 服务并获取 JSON 数组。直到这里一切都很好,但是,当我想在视图上显示值时,我什么也得不到。我像以前一样做了,不知道为什么它不起作用。

有控制器:

assets.controller('DetallTipusCtrl', function ($scope, $http, $routeParams){
                $http.get('http://10.0.203.73/WS/ws.php/tipusactius/getDetails/'+$routeParams.param).success(function(data) {
                    $scope.atrb = data;
                });
                $scope.param = $routeParams.param;    
            });

和观点:

<table class="table" ng-controller="DetallTipusCtrl">
                    <tr>
                        <th>#</th>
                        <th><a href="">Atribut</a></th>
                        <th><a href="">Mida</a></th>
                        <th><a href="">Tipus</a></th>
                    </tr>
                    <tr ng-repeat="atribut in atrb | orderBy:sortField:reverse">
                        <td></td>
                        <td>{{atribut.nomAtribut}}</td>
                        <td>{{atribut.midaAtribut}}</td>
                        <td>{{atribut.valor}}</td>
                    </tr>
                </table> 

JSON数组构造良好,但我什么也看不到,我不知道为什么,如果有人可以提供帮助的话。谢谢!

编辑:JSON:

{"nomAtribut":"fgdsgsfd","midaAtribut":"16","valor":"String","tipus_actius_idtipus_actius":"26","nom":"yiuhdfiau837629875wf"}

解决:

JSON

数组是一个 JSON 对象,这就是我迭代和显示属性的方式:

<tr ng-repeat="(key, value) in atrb">
    <td>{{value.propertie}}</td>
</tr>

你的响应不是一个数组,它是一个对象。您可以像这样循环访问对象属性:

<tr ng-repeat="(key, value) in atrb">
    <td>{{value}}</td>
</tr>

如果需要有条件地显示属性,可以检查密钥。

你应该在你的成功函数中这样做

    $http.get('http://10.0.203.73/WS/ws.php/tipusactius/getDetails/'+$routeParams.param).success(function(data) {
   $scope.$evalAsync(function(){$scope.atrb = data;});
});

你的响应不是数组

        <td>{{atrb.nomAtribut}}</td>
         <td>{{atrb.midaAtribut}}</td>
         <td>{{atrb.valor}}</td>

显示使用上面的代码

像这样调用$http

$http.get('http://10.0.203.73/WS/ws.php/tipusactius/getDetails/'+$routeParams.param)
.then(function(response) {$scope.atrb = response.data;});