[英]How to retrive data from nested JSON using API call and display it on a webpage using js
[英]Retrive Data from nested JSON and display a list using AngularJS
我目前正在使用AngularJS和JSON数据开发应用程序,但遇到了困难。 我也是Angular的新手。 这里是:
控制器代码段:
courseController.controller('LectureController', ['$scope', '$http', function($scope, $http){
$http.get('js/data.json').success(function(data){
$scope.lecture = **data.course[4].lectures**;
$scope.lectureOrder = 'videoId';
});
}]);
并且视图是:(Details.html)
<section class="artistinfo">
<div class="artist cf" ng-model="courses">
<div align="right"><a href="#/details/{{prevItem}}" class="btn btn-left"><</a>
<a href="#/details/{{nextItem}}" class="btn btn-left">></a></div>
<img ng-src="{{courses[whichItem].thumbnail}}" alt="Photo of {{courses[whichItem].name}}">
<h1>{{courses[whichItem].name}}</h1>
<div class="info">
<h3>{{courses[whichItem].description}}</h3>
<div align="right">Lectures: {{courses[whichItem].lectures.length}}</div>
<div align="center">
<a href="#/lectures/{{courses[whichItem].courseId}}">
<button>Get Started</button></div>
</a>
</div>
</div>
<a href="index.html">« Back to search</a>
</section>
(Lectures.html)
<section class="artistpage">
<ul class="artistlist" ng-app>
<li ng-animate="animate'" class="artist cf" ng-repeat="lec in lecture | orderBy: videoId">
<div class="info">
<h2>{{lec.videoName}}</h2>
<h3>{{lec.videoDetails | cut:true:160:' ...'}}</h3>
</div>
</li>
</ul>
</section>
我想从所选课程的按钮获取动态ID,而不是像data.course [ 4 ] .lectures那样获取静态ID。
JSON:
那我该怎么做呢? 演示: www.faizansaiyed.ml/EduvanceApp
我想你会做这样的事情:
$http.get('js/data.json').success(function(data) {
$scope.records = data;
$scope.whichItem = $routeParams.itemId;
并在您的html文件中,您可以这样做:
第一个html文件
<a href="#details/{{records.indexOf(item)}}" >
对于第二个html文件,您将执行此操作:例如,类似这样的操作:
<div class="col s12 error_info_box" ng-repeat="item in records[whichItem].errors">
<p><b>Error Type:</b> {{item.type}}</p>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.