我需要读取 Angular 1.6 中的 JSON 格式文件,因此我执行以下操作
在 HTML 中
<div ng-model="JsonRendering" ng-controller="NewsRendering">
<div ng-repeat="NewsData in News">
<h3>{{NewsData.Title}}</h3>
</div>
</div>
在脚本中
angular
.module( "JsonRendering", [])
.controller("NewsRendering", [ "$scope", "$http", function ($scope, $http) {
$http({
method: 'GET',
url: 'News/News.json'
}).then(function success(data) {
$scope.News = data;
});
}]);
在 JSON 中
{
"articles": [
{
"Title": "Article 1",
"Abstract": "Article 1 abstract starts here ...",
"Body": "Article 1 body starts here....."
}
]
}
我试图只呈现标题,但它不起作用。它显示在网页上{{NewsData.Title}}
。
注意: json 文件是文件夹内的本地 JSON 文件。所以json文件的路径是“Root/News/News.json 那么我该怎么做才能解决问题
将其更改为,您应该使用 data.data
.controller("NewsRendering", [ "$scope", "$http", function ($scope, $http) {
$http({
method: 'GET',
url: 'News/News.json'
}).then(function success(data) {
$scope.News = data.data;
});
}]);
你的 ng-repeat 应该是,
<div ng-repeat="NewsData in News.articles">
<h3>{{NewsData.Title}}</h3>
</div>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句