Utilize AngularJS ng-repeat directive to refine JSON objects

I'm working on an angular js controller with a json array that organizes countries by continents. Each continent consists of its own array of countries.

 //CONTROLLER
app.controller('CountryController', function(){

    this.continents = 
    [
         {
                name:'Africa',
                countries:
                [
                    { name: 'Algeria', code:'DZ', operators:'3'},
                    { name: 'Angola', code:'AO', operators:'1'},
                    { name: 'Benin', code:'BJ', operators:'2'},
                    { name: 'Burkina Faso', code:'BF', operators:'1'}
                ],
          },


         {
               name:'AsiaPacific',
                countries:
                [
                    { name: 'Afghanistan', code:'AF', operators:'4'},
                    { name: 'Bahrain', code:'BH', operators:'2'}
                ],
          }
    ];

});

In the view, I want to display these countries in tabs categorized by continent. For example, there should be a tab for Africa displaying all countries within the Africa array. However, my attempts at using the 'filter' method to show countries from specific continents have not been successful. Upon inspecting the element, it appears that the code is commented out. This project marks my first experience using Angular-js, so I'm still navigating through the learning curve.

This is the current structure of my view:

       <div ng-app="countriessupported" class="container container_with_height">
                    <div ng-controller="CountryController as countryCtrl" id="myTabContent"  class="tab-content hidden-xs">
                             <div class="tab-pane fade active in" id="africa">
                                   <div class="col-md-12 countries_col-12" ng-repeat="continent in countryCtrl.continents.name | filter: 'Africa' ">

                                             <a href="">
                                                <div class="col-md-3 country_container" ng-repeat="country in continent.countries">

                                                  <div class="country_name">
                                                      {{ country.name }}
                                                   </div>
                                                </div>
                                           </a>

                                   </div>
                           </div>
                        </div>
            </div>

The goal is to have the countries displayed within the div with class col-md-3. What steps can I take to achieve this successfully?

Answer №1

Give this HTML snippet a try

<div ng-app="countriessupported" class="container container_with_height">
    <div ng-controller="CountryController as countryCtrl" id="myTabContent"  class="tab-content hidden-xs">
        <div class="tab-pane fade active in" id="africa">
            <div class="col-md-12 countries_col-12" ng-repeat="continent in countryCtrl.continents | filter: 'Africa' ">
                <a href="">
                    <div class="col-md-3 country_container" ng-repeat="country in continent.countries">
                        <div class="country_name">{{ country.name }}</div>
                    </div>
                </a>
            </div>
        </div>
    </div>
</div>

Feel free to reach out if you need further assistance

Answer №2

Consider updating your initial repeater to

ng-repeat="continent in countryCtrl.continents"
rather than
ng-repeat="continent in countryCtrl.continents.name"
. This adjustment should ensure that the filter functions correctly.

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

What is the method for writing the following line in CSHTML server-side code?

<script> function a(id) { var table = document.getElementById(id); .... } </script> @{ //Is there a way to rewrite the line "var table = document.getElementById(id)" here within the ser ...

Implementation of asynchronous code in event handlers is necessary

In my AngularJS controller, I have implemented an event mechanism to communicate with external elements. One particular event is triggered to retrieve the URL where the data should be sent. The event handler reacts to this event by making an AJAX call usi ...

The onClick event for "history.go(0)" is functional in Internet Explorer, but it does not work in Mozilla Firefox. What can be done to address this problem specifically for Mozilla browser?

After experimenting with different browser compatibility, I have discovered a way to clear all fields when clicking the "New" button. By using the code onClick="history.go(0)", the fields are successfully emptied in IE but unfortunately fail in Mozilla. ...

Harnessing the power of two-way data binding in VueJS

I am looking to utilize Vue's two-way data binding to dynamically update the values of amount and total. The price of a given product is fixed. When users modify the amount, the total = amount * total will be automatically calculated. Similarly, users ...

How can I implement a page switch in vuejs by clicking on a name in a table list?

I'm currently working on a list page in VueJS and facing some challenges. Here is the code snippet: <template> <vue-good-table :columns="columns" :rows="row" :search-options="{ ...

Perform a fetch request within a map function or loop

I'm currently working on iterating over an array of objects. Depending on the content of some of those objects, I need to execute a fetch function and wait for the result before updating the new array. Below is my JavaScript code: const allPosts = ...

Discover the power of Snowflake's lateral flatten function in exploding multiple JSON values within nested JSON lists!

I have a variant table containing 24 JSONs that look like the following (one per row): { "context": "marketplace", "metadata": { "app_version": "1.0.4 (166)", }, "params": { " ...

Is AngularJS Experiencing Bugs with the Webcam getUserMedia API?

I recently created a webcam directive in AngularJS that utilizes a service. For reference, I followed this example: Surprisingly, the example works perfectly on my tablet, but when I integrate the code into my tablet's Google Chrome browser, it encou ...

Positives and negatives images for accordion menu

I have successfully created an accordion list using HTML, CSS, and JavaScript. However, I would like to enhance it by adding a plus and minus picture in the left corner of the heading. Is there a way to achieve this functionality? I have two images that I ...

Adjust choices in a dropdown menu based on the selection of another dropdown menu

I am attempting to create a scenario where selecting an option from one dropdown list will dynamically change the options available in the next dropdown list. You can find my code on jsfiddle <!DOCTYPE html> <html> <body> &l ...

Retrieve the script's location from the server prior to the initialization of Angular

Is there a way to dynamically add a script in the index.html page based on the application version? I have a controller that retrieves the app version and attempted to achieve this using AngularJS: var resourceLoader = angular.module('MyTabs&apo ...

How to set the value of an `<input type="date"` in jQuery

Having trouble figuring out why this code isn't functioning properly. The input field I'm using is a simple 'input type="date"' setup like this.... <input type="date" name="Date"/> I am attempting to have the value automatically ...

Tips for indicating which content to display on template literals

Utilizing template literals to showcase some fetched data, I've successfully displayed all the necessary information on the frontend. However, certain content is hidden and meant to be toggleable. Yet, I'm struggling to figure out how to link eac ...

From storing data in a JSON file on Drive to importing it into Google

I have a couple of JSON files saved on my Google Drive that I want to extract data from and import into a spreadsheet. I attempted to use the ImportJson function from github, but it's designed to fetch JSON files directly from an API, which is not app ...

I'm unsure about the JavaScript toolkit framework's handling of selecting HTML class attributes

I have been exploring the Electron Framework using a JavaScript toolkit known as Xel. In my main.js file, I am working with the following syntax: document.querySelector("menu.selected").className.remove('selected') In my Xel code snippet, I hav ...

Transforming an array of strings to integers within a GraphQL query when they are incorporated

I need help finding a solution to pass an array of strings and embed it into a query while using React and GraphQL. The issue I'm facing is that even though the parameter is accepted as an array of strings, it gets converted to a string when embedded. ...

The Push Over Menu is malfunctioning and not functioning properly

I am facing an issue with pushing my menu along with the content to the right. The JS code I have is not working as expected. When I click on <div class="menu-btn toggle"></div>, the menu does not trigger. Can someone help me understand why thi ...

Is there a way to route an angular request through a spring filter prior to validation?

I'm currently working on implementing an XSS Filter in a backend application (built with JAVA and Spring). The goal is to validate input values from a frontend app (AngularJS) against the filter before proceeding to validate the rest of the content. ...

When the fragment view pager is replaced, the new viewpager fragment appears empty

When I try to call the view pager fragments by replacing them over a frame layout, I encounter an issue where the last loaded fragments of the view pager appear blank on subsequent calls. //Below is the code snippet where I replace the view pager fragment ...

The AngularJS ngModelController is a powerful tool for managing

How can I accurately check ngModelController validity? Within my directive, I have a controller object. When I console.log the object from inside the directive, it displays: console.log(ctrl) $dirty: false $invalid: true $modelValue: "" $name: undefined ...