我是 ionic/angularjs 新手,我需要知道如何从 Json url 将数据显示到 HTML View 。
所以,我的 Json URL 中的数据如下所示:
{
News: [
{
id: "1",
title: " It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.",
image: "uploads/news/fortunaglobal_logo.jpg",
status: "yes"
},
{
id: "2",
title: "fgdf",
description: "hhjgh",
image: "uploads/news/16613_10204428208286459_7484489390034618482_n.jpg",
status: "yes"
}
]
}
我在我的脚本中尝试过这个:
.controller('NewsCtrl', function($scope, $http) {
$http.get(' "JSON URL" ')
.success(function(response, status, headers, config){
window.alert('came in');
if(status==200){
$scope.news = response.data['News'];
window.alert($scope.news );
} else {
window.alert('No internet Connection');
}
console.log(news);
})
.error(function(data,status,headers,config){
window.alert('error '+data+status);
console.log(data);
console.log(status);
});
})
这在我的 html 页面中
<ion-view title="News" ng-controller="NewsCtrl">
<ion-nav-buttons side="left">
<button menu-toggle="left" class="button button-icon icon ion-navicon"></button>
</ion-nav-buttons>
<ion-content class="has-header" scroll="true" padding="true">
<ul ng-repeat="newsS in news">
<li>{{newsS.title}}</li>
</ul>
</ion-content>
</ion-view>
有人可以向我解释一下我做错了什么吗?
最佳答案
由于您不够具体,我只能猜测您想要表达的意思,但我看到一些可能有问题的事情:
$scope.news = response.data['News'];
如果上面描述的 JSON 是完整响应,则此数据应直接包含在您的响应元素中(根据 Angular 文档,实际上应该将其命名为 data)。
所以试试这个:
$scope.news = response.News;
接下来无法工作的是
console.log(news);
你的变量news
没有定义,也许你想使用$scope.news
?
关于javascript - 使用 AngularJS 在 JSON URL 中显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26156166/