JSONP & Angular 添加额外的空数据
JSONP & Angular Adding Extra Empty data
一切正常,但是当没有其他数据时,我得到了额外的 5 行空数据:
示例:
Sat, Apr 25 2015 Madison Square Garden, New York, New York, USA
Wladimir Klitschko 63 vs Bryant Jennings 19
vs
vs
vs
vs
vs
Sat, May 09 2015 Minute Maid Park, Houston, Texas, USA
James Kirkland 32 vs Saul Alvarez 44
vs
vs
vs
vs
vs
我的 JS:
function WidgetCtrl($scope, $http) {
$scope.items = [];
$http.jsonp("http:/domain.com/schedule?callback=JSON_CALLBACK").success(function(data)
{
$scope.items = data;
}).error(function(data, status, headers, config) {
$scope.status = status;
});
}
index.html
<div id="widget-content" ng-controller="WidgetCtrl">
<div ng-repeat="item in items">
{{item.searchdate}} {{item.event_place}}
<div class="widget-list">
<div ng-repeat="name in item">
{{name.boxer1_name}} {{name.boxer1_w}} vs {{name.boxer2_name}} {{name.boxer2_w}}
</div>
</div>
</div>
</div>
JSON:
[
{
"event_id":"1821",
"searchdate":"Sat, Apr 25 2015",
"event_place":"Madison Square Garden, New York, New York, USA",
"networks":"TV: HBO Boxing",
"time":"Time: 9pm Et",
"0":{
"match_id":"5068",
"match_name":"Heavyweight",
"mainevent":"main",
"body":"",
"boxer1_id":"7",
"boxer1_name":"Wladimir Klitschko",
"boxer1_w":"63",
"boxer1_l":"3",
"boxer1_d":"0",
"boxer1_ko":"53",
"boxer2_id":"2780",
"boxer2_name":"Bryant Jennings",
"boxer2_w":"19",
"boxer2_l":"0",
"boxer2_d":"0",
"boxer2_ko":"10"
}
},
{
"event_id":"1853",
"searchdate":"Sat, May 09 2015",
"event_place":"Minute Maid Park, Houston, Texas, USA",
"networks":"TV: HBO",
"time":"Time: 9pm ET",
"0":{
"match_id":"5127",
"match_name":"super welterweight",
"mainevent":"main",
"body":"",
"boxer1_id":"233",
"boxer1_name":"James Kirkland",
"boxer1_w":"32",
"boxer1_l":"1",
"boxer1_d":"0",
"boxer1_ko":"28",
"boxer2_id":"1336",
"boxer2_name":"Saul Alvarez",
"boxer2_w":"44",
"boxer2_l":"1",
"boxer2_d":"1",
"boxer2_ko":"31"
}
}
]
我认为问题出在您的第二次 ng-repeat 中 "name in item"。它只是遍历 6 条数据(event_id、searchdate 等),它在其中找到拳击手数据的唯一一条是名为“0”的项目。根据您的数据,您应该使用
<ng-repeat="name in item.0">
因为您的拳手数据包含在 item.o 中而不是项目中。
三天后我解决了这个问题。我将 JSON 提要从“0”更改为 "match"。谢谢斯科特。不知何故,你的回答帮助我解决了这个问题。
一切正常,但是当没有其他数据时,我得到了额外的 5 行空数据:
示例:
Sat, Apr 25 2015 Madison Square Garden, New York, New York, USA Wladimir Klitschko 63 vs Bryant Jennings 19 vs vs vs vs vs Sat, May 09 2015 Minute Maid Park, Houston, Texas, USA James Kirkland 32 vs Saul Alvarez 44 vs vs vs vs vs
我的 JS:
function WidgetCtrl($scope, $http) {
$scope.items = [];
$http.jsonp("http:/domain.com/schedule?callback=JSON_CALLBACK").success(function(data)
{
$scope.items = data;
}).error(function(data, status, headers, config) {
$scope.status = status;
});
}
index.html
<div id="widget-content" ng-controller="WidgetCtrl">
<div ng-repeat="item in items">
{{item.searchdate}} {{item.event_place}}
<div class="widget-list">
<div ng-repeat="name in item">
{{name.boxer1_name}} {{name.boxer1_w}} vs {{name.boxer2_name}} {{name.boxer2_w}}
</div>
</div>
</div>
</div>
JSON:
[
{
"event_id":"1821",
"searchdate":"Sat, Apr 25 2015",
"event_place":"Madison Square Garden, New York, New York, USA",
"networks":"TV: HBO Boxing",
"time":"Time: 9pm Et",
"0":{
"match_id":"5068",
"match_name":"Heavyweight",
"mainevent":"main",
"body":"",
"boxer1_id":"7",
"boxer1_name":"Wladimir Klitschko",
"boxer1_w":"63",
"boxer1_l":"3",
"boxer1_d":"0",
"boxer1_ko":"53",
"boxer2_id":"2780",
"boxer2_name":"Bryant Jennings",
"boxer2_w":"19",
"boxer2_l":"0",
"boxer2_d":"0",
"boxer2_ko":"10"
}
},
{
"event_id":"1853",
"searchdate":"Sat, May 09 2015",
"event_place":"Minute Maid Park, Houston, Texas, USA",
"networks":"TV: HBO",
"time":"Time: 9pm ET",
"0":{
"match_id":"5127",
"match_name":"super welterweight",
"mainevent":"main",
"body":"",
"boxer1_id":"233",
"boxer1_name":"James Kirkland",
"boxer1_w":"32",
"boxer1_l":"1",
"boxer1_d":"0",
"boxer1_ko":"28",
"boxer2_id":"1336",
"boxer2_name":"Saul Alvarez",
"boxer2_w":"44",
"boxer2_l":"1",
"boxer2_d":"1",
"boxer2_ko":"31"
}
}
]
我认为问题出在您的第二次 ng-repeat 中 "name in item"。它只是遍历 6 条数据(event_id、searchdate 等),它在其中找到拳击手数据的唯一一条是名为“0”的项目。根据您的数据,您应该使用
<ng-repeat="name in item.0">
因为您的拳手数据包含在 item.o 中而不是项目中。
三天后我解决了这个问题。我将 JSON 提要从“0”更改为 "match"。谢谢斯科特。不知何故,你的回答帮助我解决了这个问题。