Explore a JSON structure by clicking on a link using AngularJS

I am looking to develop a library in AngularJS that includes dropdown menus for selecting a book and then choosing a chapter from that book. Once a chapter is selected, the corresponding text should be displayed. All book data is available in a single JSON format. How can I implement a navigation link to move to the next chapter, as shown in the image located at the bottom left corner?

Image: First, select a book and then choose a chapter from the dropdown menu

// index.html
<div ng-controller="ArticlesCtrl">
   <br/>
   <div class="dropdown">
      <form class="form" name="myForm">
         <label for="mySelect">Select a Book:</label>
         <select class="dropdown-select" name="mySelect" id="mySelect" ng-options="option.bname for option in articles" ng-model="articles.bname" ng-change="updateBook()"></select>
      </form>
   </div>
   <div class="dropdown">
      <form class="form" name="myForm" ng-show="chapters" class="ng-hide">
         <label for="mySelect">Choose a chapter:</label>
         <select class="dropdown-select" name="mySelect" id="chapter" ng-options="option.Icnumber for option in selected" ng-model="selected.Icnumber" ng-change="updateChapter()"></select>
      </form> 
   </div>

   <hr>

   <p ng-repeat="paragraph in paragraphs track by paragraph.Ivnumber">{{paragraph.Ivnumber}} {{paragraph.Itext}}"</p>

   <a>Next Chapter</a>

</div> 

app.js

.controller('ArticlesCtrl', function($scope, $location, $http, Cart){
$scope.cart = Cart;
$http.get('lib.json').then(function(articlesResponse) {
  $scope.articles = articlesResponse.data;
  $scope.chapters = false;
  $scope.paragraph = false;
  $scope.show = false;
  $scope.updateBook = function() {
    var item = $scope.articles.bname.Ibnumber;
    var indexItem = item - 1;
    $scope.selected = articlesResponse.data[indexItem].CHAPTER;
    $scope.chapters = true;
  }
  $scope.updateChapter = function() {
    var item = $scope.articles.bname.Ibnumber;
    var indexItem = item - 1;  
    var chapter = $scope.selected.Icnumber.Icnumber;
    var indexChapter = chapter - 1;
    $scope.paragraphs = articlesResponse.data[indexItem].CHAPTER[indexChapter].PARAGRAPH;
    $scope.paragraph = true;
  }
});
})

Thank you.

Answer №1

If you want to improve your code, consider making the updateChapter function more versatile.

<a ng-click="updateChapter(selected.Icnumber + 1)">Move to Next Section</a>

Adapt the updateChapter function as needed:

$scope.updateChapter = function(chapter) {
    var Item = $scope.articles.bname.Ibnumber;
    var indexItem = Item - 1;
    // Update the selected menu
    $scope.selected.Icnumber = chapter;
    var indexChapter = chapter - 1;
    $scope.paragraphs = articlesResponse.data[indexItem].CHAPTER[indexChapter].PARAGRAPH;
    $scope.paragraph = true;
}

Also, change ng-change="updateChapter()" to

ng-change=updateChapter(selected.Icnumber)"

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

Issue with displaying parsed data using JSON and jQuery

I am experiencing an issue with a function I have written $.post('php/client.login.php', {username:username, password:password}, function(json){ var ids = json; alert(json.id); }, 'json'); The ...

Hitting the enter key to choose a value from a dropdown menu will automatically submit the form

I've encountered an issue while using ui-select. Whenever I press enter to select searched results, the selection is made and the form automatically submits. I'm having trouble figuring out what the problem is. Here is the HTML code snippet I am ...

Steps for retrieving a table of records with Jquery and sending it to the server through AJAX

Displayed below is a table I have: <table cellspacing="0" rules="all" border="1" id="ContentPlaceHolder1_GridView1" style="border-collapse:collapse;" class="table table-striped"> <tbody> <tr> <th scope="col"> ...

Error: The system encountered an unexpected character (92) while parsing the JSON file [Java]

Apologies for the messy title, but I'm completely puzzled by this issue. I'm attempting to parse a JSON String using Jackson. The code is simple: import com.fasterxml.jackson.databind.ObjectMapper; import formatter.Tweet; import com.fasterxml.j ...

Is there a way to utilize JavaScript and AJAX to save a WordPress cookie and log in a user, all without the need for any plugins?

My goal is to log in to WordPress using only ajax requests. Here's the code I have so far: var username = data.username; var password = data.password; var wp_login_url = "http://local_Ip/api/user/generate_auth_cookie/?username=" +username + "&pas ...

Angular Form Group without a name

Struggling to loop through my formgroups to identify which ones are valid and add the invalid ones to a new array. I can't seem to grab the name value for each formgroup. this.applicationFormArray = new FormGroup({ selectAppFormGroup:th ...

javascript varying functionality between Chrome and Firefox

My Grease monkey script/Tamper monkey is designed to click on buttons that contain the word 'attach'. Although it works perfectly, I have noticed a difference in behavior between Chrome and Firefox. In Firefox, the clicks occur in the order of a ...

Can we expect React's useState to wait before executing subsequent logic, or will it immediately update the state and trigger a re-render

I have a specific scenario in my code where I am using the useState setter function in React to update a value on line 5. Everything seems to be functioning well without any errors, but I'm curious about something. Since setState updates the state and ...

"Step-by-step guide on showcasing a specific blog post using its unique identifier in an Angular/Express/MongoDB application

I'm struggling to figure out how to retrieve a single blog post by its ID, especially since I am new to this. Currently, my main blog application has an ng-repeat functionality that fetches all posts. What I really want is the ability to click on a p ...

Troubleshooting: ngModel in Angular 2 Component does not properly update the parent model

I have been attempting to develop a wrapper component for input elements like checkboxes, but I am facing an issue where the parent variable (inputValue) is not updating even though it has been defined as an ngModel. The code snippet for my component look ...

Error encountered in jQuery validation: Uncaught TypeError - $(...) ""valid is not a function" is displayed with proper references and order

When I try to call a function, I keep getting an error message that says "Uncaught TypeError: $(...).valid is not a function"... JS $('input[data-val=true]').on('blur', function() { $(this).valid(); }); HTML <div class="col-x ...

Error 414: The URL exceeds the maximum length and cannot be processed

I am currently utilizing vuejs and sending an axios request to the server in order to download a csv file. download() { var that = this //this.records = [{id: 1, name: 'Jack'}, {id: 2, name: 'Jacky'}, {id: 3, name: &apos ...

Is it possible to implement MV* in Polymer using models and services as polymer elements?

Imagine I want two views (polymer-elements) to share a model, how can this be achieved? In Angular, the model would reside in a singleton service that is injected into the views, allowing both views to access the same data source. I attempted to replicat ...

"Implement image uploading and retrieval functionality in your application by utilizing Parse and Back4App with the help of Node

It seems like I have a question that may have already been answered, but I can't find the right solution for my issue. I'm facing trouble with my code and can't figure out what's wrong. The problem arises when I try to upload specific ...

Sending a path containing '/' to either the $resource or $http factory

Is there a way to send a parameter containing / to the Factory? Trying to achieve something like this: .factory('MyData', ['$resource', function ($resource) { return $resource('http://1.2.3.4/:urlFragment', { urlFragmen ...

Finding matches within a specific group in regular expressions

I am currently tackling the challenge of implementing a feature that involves detecting and linking phrases like "Co. Reg. No" in a specific HTML element. <div class="entry">The company with Co. Reg. No 1241515 will...</div> My goal is to cre ...

Tips for overcoming the Chrome Extension Error related to the "script-source 'self'" issue

I've been developing a Chrome extension that uses goo.gl to shorten URLs. Here is the code I'm currently working with: $("#ajaxfiller").text(""); //Retrieve entered URL var longUrl = $("#input2").val(); longUrl = '"' + longUrl + &a ...

Get information in one service that was broadcasted from a different service

In my controller, I am invoking a service called printService which in turn calls another service named dataService that has an emit method. I am looking to capture the data emitted by dataService within printService. As a beginner in AngularJS, I would a ...

obtain the specific attributes of the button that was clicked using jQuery

My goal is to preview an image before uploading it to the server, and this is the HTML code I am using: <html> <script> function readURL(input) { if (input.files && input.files[0]) {.......}} $("#selectedFile").change(functio ...

Is there a way for me to automatically close other menus when the current menu is open using slideToggle?

Seeking assistance with enhancing the navigation functionality. How can I ensure that when a user clicks on a menu item, any other open menus are closed and the active class is removed from them? The current navigation setup meets my requirements except ...