即使存在html元素,ngrepeat中也没有内容

时间:2016-10-21 20:47:58

标签: html angularjs angularjs-ng-repeat

我从API获取数字列表,并使用ng-repeat在网页上显示。对于10个数字,会创建10个元素,但内容为空。我尝试了很多不同的组合,但内容并没有显示出来。 HTML元素看起来像enter image description here

如我所示,使用ngrepeat按钮,但缺少文字。

代码:

    $scope.getParts = function(){
$http({
    method:"GET",
    url:'v1/getpartsname'
}).then(function(res){
    $scope.parts = [];
    for (var i = 1; i < res.data.length; i++) {
        $scope.parts.push(parseInt(res.data[i][0].trim()));
    }
   // $scope.partNumbers = parts;
    $scope.searchPart = "";
});
}

html是

<div className="container" ng-show="showPartNumbers" ng-init="getParts()">
        <div className="row">
            <div className="col-sm-6" ng-repeat="part in parts| filter:searchPart">
                <button type="button" ng-click="home(part)">{{part}}</button>                   
            </div>
        </div>
</div>

我将html显示如下:

enter image description here

我与范围绑定的格式化json是:

enter image description here

我从http电话获得的原始json是:

enter image description here

2 个答案:

答案 0 :(得分:1)

@SangitDhanani当然是羽毛球的作用,因为你已经删除了那个破碎的位。一种方法是将从API调用中返回的json保存到json文件中,然后将其放入plunker中。

它会让侦探者更接近您的问题,并希望帮助我们找到来源。

我曾经多次看到的是REST API返回JSON的地方,但不是JSON,而是字符串化,需要JSON.parse来解压缩它。像

这样的东西
response: '{"id":"1234","name":"Mikkel"...

乍一看它看起来像JSON,但实际上它是一个字符串。

答案 1 :(得分:0)

parseInt可能失败了(其中一个值为空/无效?),尝试使用普通值,即没有parseInt或trim:

$scope.parts.push(res.data[i]);

您也可以尝试将数据放入范围变量

$scope.rawData = res.data;

然后在UI中显示

{{rawData | json}}

虽然您可以查看检查器中的数据以确保结构

相关问题