Access information from deeply nested JSON objects and present it in a formatted list using AngularJS

Currently, I am facing a challenge while working on an AngularJS app that utilizes JSON data. As a beginner with Angular, I encountered a point of confusion. Here is the issue:

Below is the snippet from the controller:

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';
    });
}]);

Here is the view code (Details.html):

<section class="artistinfo">
  <div class="artist cf" ng-model="courses">
    <div align="right"><a href="#/details/{{prevItem}}" class="btn btn-left">&lt;</a>
    <a href="#/details/{{nextItem}}" class="btn btn-left">&gt;</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">&laquo; Back to search</a>
</section>

The other view code (Lectures.html) is as follows:

<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>

Instead of having a static ID like in data.course[4].lectures, I want to extract a dynamic ID from a button related to the selected course.

For reference, here is the link to the demo: www.faizansaiyed.ml/EduvanceApp

Answer №1

Perhaps you could approach it this way:

$http.get('js/data.json').success(function(data) {
    $scope.records = data;
    $scope.whichItem = $routeParams.itemId;

Then in your HTML file, consider the following:

For the first HTML file:

<a href="#details/{{records.indexOf(item)}}" >

And for the second HTML file, try something like this:

<div class="col s12 error_info_box" ng-repeat="item in records[whichItem].errors">

      <p><b>Error Type&#58;</b>&nbsp;&nbsp;{{item.type}}</p>





</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Problem with Pathjs routing

Can anyone help with this routing issue I'm having? Path.map("/(:page_1)(/:page_2)/").to(funct); The route is not matching for: /projects/index2/ It matches /anything, but not /anything/anything If you have any ideas, please share! ...

How can I simultaneously add two elements to a single node or to a node that does not exist in the JSON file?

I thought this would be simple, but I'm struggling to find the answer... var crit = { 'webshopId': webshopId, 'type': 'product'} }; I need to include sku.regularPrice = { $gte : parameters.minPr ...

Learning to access a base64 encoded PDF file using JavaScript

var ajaxSettings = { url: urls.orders.list+"/"+singlePacket.requests[0].order_id+"/labels", //retrieve labels with ShipperAssigned status type: "GET", contentType: "application/json", headers: { "Authorizatio ...

Error message: The Liferay JavaScript Function has not been defined

I am a newcomer to Liferay and have been attempting to utilize Ajax within my scripts, but unfortunately, the code does not seem to load correctly in the browser. I even tried testing it by simply adding an alert. Every time I try, I encounter the "functi ...

Search the table for checked boxes and textboxes that are not empty

Could you suggest alternative ways to express the following scenario? I have a table with 3 rows. Each row contains a column with 3 checkboxes and another column with just a text box. I would like it so that when the values are retrieved from the database ...

Transform an object containing an array into a standalone array

After deserialization, I am facing a problem where my JSON is converted into an object with another nested object called "Content". This Content object contains an array that needs to be converted into an object as well: object Content = {[ { ...

Having difficulties retrieving the <td> id using jQuery

I am a beginner in the world of JavaScript and jQuery, and I am currently attempting to extract both the ID and the value of an element. Here is my initial AJAX request: $.ajax({ type: "POST", url: "Home/AddText", data: JSON.stringify({ te ...

Spin and flip user-submitted images with the power of HTML5 canvas!

I am currently working on a profile upload system where we are implementing image rotation using the HTML5 canvas object with JavaScript. Although the uploaded image is rotating, we are facing issues where parts of the image are being cut off randomly. So ...

Developing microfrontends using Angular involves loading and navigating a compiled package from npm

I have been struggling to find a solution to an ongoing issue and wasting time on futile attempts. Currently, I am working with Angular 15 within a microfrontend architecture. My goal is to implement a system where I can download a compiled microfrontend ...

Unable to integrate a new third-party script into a Next.js application

In my attempt to integrate the following script, I have tried adding it first to _document.js, then to app.js, and finally to a specific page. <Script src="https://anywebsite.ai/chatbot/chatbot.js"></Script> <Script id=" ...

typescript: Imported modules in typescript are not functioning

I'm facing an issue where I installed the 'web-request' module but unable to get it working properly. Here is my code: npm install web-request After installation, I imported and used it in my class: import * as WebRequest from 'web-r ...

Using InnerHTML in Javascript within the Quasar/VueJS framework is unsupported

I am looking to dynamically create tables based on the items inside the 'counts' array below. The number of tables and their contents can change. Here is my divContainer, where the innerHTML will be appended: <div id="divContainer" style="pa ...

Having trouble with exporting static HTML using Next.js

Recently, I have completed a project and now I am looking to export it to static HTML. In order to achieve this, I added the following command in my package.json file: build" : "next build && next export Upon running the command npm run ...

Unable to retrieve button value with material-ui package

My task requires me to retrieve the value of a button, as it contains an ID essential for further steps. Initially, I used a standard button with Bootstrap styling and everything functioned correctly. <button value={row.vacationRequestID} c ...

What inspired the Angular JS Team to name their end-to-end testing tool Protractor?

Have you ever wondered why the name "protractor" was chosen for the end-to-end testing tool in AngularJS? ...

I am encountering a situation while performing a Delete Jquery Ajax request where the response code is 200, however, the data is not being successfully deleted. What are some potential explanations for this issue?

Having issues with a DELETE request in my code. Here is what I have so far: $.ajax({ type : "DELETE", url : "/meds/dme-rest-api/resources/data-setup/deleteCheckList.json?" + $.param({"checkListId":currentCheckListId}), headers : {authToken : ...

Learn how you can swap out UI elements instead of simply appending new ones onto the existing interface. Discover the power of JavaScript, the Fetch API, and Dom

Currently, I am working on a small project to learn more about APIs and how to interact with them effectively. This project involves searching for and displaying characters from a TV show using data obtained from an API. My issue arises when I try to make ...

An error occurs when attempting to assign a value to a MUI file TextField

Struggling with setting the value of a MUI Textfield that has type="file" props, resulting in the following exception being thrown: Uncaught DOMException: An attempt was made to use an object that is not, or is no longer, usable Interest ...

Unable to access Form element in Firefox browser

I'm struggling with debugging unfamiliar problems. For instance, in my HTML there's a form: <form name="myForm" > <table> <tr> <td> <input type="radio" name="myType" value="val" onclick="someF ...

What could be the reason behind the ineffectiveness of my recently acquired Google Maps API key

For years, I had a function working with the Google API flawlessly. However, after obtaining a new API key, everything seems to have gone haywire. The issue is that there is no output after the `alert(address)` line because the code from `geocoder.geocod ...