How to manage the three states (InProgress, Success, Error) of a dropdown dynamically in AngularJS with ng-show/hide?

Currently, I have implemented a bootstrap dropdown that loads a list of countries by making an external API call. My goal is to display three different states of behavior (progress, success, error) in the dropdown using ng-show/hide. However, I am unsure how to dynamically change ng-show/hide within a dropdown as described below:

1. In Progress (loadCountry =='progress' - Displaying the "Loading..." text)

2. Binded Successfully (loadCountry=='success' - Binding the API response of the country's list)

3. Error Message (loadCountry=='error' - Displaying an error message if any error occurs during the API data retrieval)

I am confused about how to include the behavior for "In Progress" and "Error" in the bootstrap dropdown using ng-show. In my case, the value for the loadCountry scope variable is automatically set to progress, success, or error based on the flow.

<div ng-show="loadCountry== 'success'>
  <button class="btn dropdown-toggle" type="button" id="btnCntry" 
       data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
        {{selectedCountry}}
        <span class="caret"></span>
  </button>
  <ul class="dropdown-menu" aria-labelledby="btnCntry">
     <li ng-repeat="country in countryList">
             <a>{{country.name}}</a>
  </ul>
</div>

Answer №1

To control the display of content in your dropdown, you can utilize the ng-show directive with the variable loadCountry:

<ul class="dropdown-menu" aria-labelledby="btnCntry">
    <div ng-show=" loadCountry == 'progress' ">Loading countries...</div>
    <div ng-show=" loadCountry == 'error' ">There was an error while loading countries :(</div>
    <li ng-repeat="country in countryList">
        <a>{{country.name}}</a>
    </li>
</ul>

I have not included an ng-show directive inside the <li> tag because it may not be necessary if the countryList is empty. However, if you wish to hide it for aesthetic reasons, you can add

ng-show=" loadCountry == 'success' "
inside the tag.

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

Verify if session is in existence

Currently in the process of setting up my NodeJS and Express App, utilizing Passport for authentication through Google Sign In and Login. All functionalities work flawlessly when tested on localhost. The sign-in process is smooth, and upon checking, I can ...

Encountering an error: [nsIWebProgressListener::onStatusChange] when utilizing jQuery AJAX within a click event?

Greetings! I am currently learning how to implement AJAX with jQuery to load an HTML document into a div element within another HTML document. Here is the approach I am using: function pageload() { $.ajax({ url: 'Marker.aspx', ...

The JavaScript exec() RegExp method retrieves a single item

Possible Duplicate: Question about regex exec returning only the first match "x1y2z3".replace(/[0-9]/g,"a") This code snippet returns "xayaza" as expected. /[0-9]/g.exec("x1y2z3") However, it only returns an array containing one item: ["1"]. S ...

"Creating a CSS3 animation for changing the background image source - a step-by-step

Unfortunately, the background-image cannot be animated with keyframes in the following way: @keyframes kick{ 0%{background-image: url(images/img-man-1.png);} 25%{background-image: url(images/img-man-2.png);} 50%{background-image: url(images/im ...

The Array.push method is part of the Array class, while the $.inArray function returns the value "false"

I have two sets of elements and I am looking to combine the values from the first set with the second one. My approach involved using the .push method. Here is how I initialized the arrays: <script> var primarySet = ["Apple", "Banana"]; var seconda ...

"Counting the clicks on the filter button in React

I have: var RightPanel = React.createClass({ componentDidMount: function () { this.load(); }, load: function(){ }, render: function () { return ( <div> <div className="row"> ...

Tips for showcasing a dataset within a table using Angular.js ng-repeat

I am encountering an issue where I have to present an array of data within a table using Angular.js. Below is an explanation of my code. Table: <table class="table table-bordered table-striped table-hover" id="dataTable" > <tbody> ...

AngularJS ng-view doesn't refresh content autonomously

I'm puzzled as to why ng-view isn't updating data changed from the controller. Here is the code snippet: $scope.reloadUserInfo = function() { APIs.tot_of(null).then(function(result) { $scope.tot_of = result; APIs.info_of(null ...

What could be causing SVG to not render in a NEXTJS project upon deployment on Vercel?

Recently, I created a standout single-page nextJS application that was beautifully styled with Tailwind CSS. One interesting feature I added was incorporating the background of a component called SectionWrapper as an svg file. To achieve this, I crafted a ...

Achieving Automatic Scrolling of Drawer in Material UI React JS

Looking for assistance, can anyone lend a hand with this code snippet I've been working on? See it here: https://codesandbox.io/s/5xoj9zw56l I referenced this code as part of my project development: https://codesandbox.io/s/6jr75xj8y3 ...

Is there a problem with textbox support for multi-line strings?

I'm having trouble getting a textbox to display multiple lines of text. For instance, when I copy three lines of text from Microsoft Word and paste it into the textbox, only the first line appears. The other two lines are not showing up, and I'm ...

A JavaScript function written without the use of curly braces

What makes a function good is how it is declared: export declare class SOMETHING implements OnDestroy { sayHello() { // some code to say hello } } However, while exploring the node_modules (specifically in angular material), I stumbled up ...

What is the most efficient way to extract a key and its corresponding value from an object containing only one pair?

Currently, I am developing a recipeBox application using React JS. Within this application, there is a state defined as: this.state = { recipeArray: [] ...} Users have the ability to add recipes by pushing objects {recipe_Name : Ingredients} into tha ...

Ways to access the req.user object within services

After implementing an authentication middleware in NestJs as shown below: @Injectable() export class AuthenticationMiddleware implements NestMiddleware { constructor() {} async use(req: any, res: any, next: () => void) { const a ...

Having trouble with my CSS hover not functioning properly

I need some help with my code. Can you please take a look and let me know why the hover effect is not working? Thank you! <style> #moreDiscussHome:hover{ background-color: #ffffff; } </style> <a id="moreDiscussHome" style="co ...

Built-in validator in Bootstrap 4 - Error messageText

I used Bootstrap 4 to create a basic form and decided to utilize the built-in validator in BS4. For the email field, I implemented the following code: <label for="email">E-mail cím:</label><br> <input type="email" name="email" class ...

Exploring the Power of JQuery Load and CSS styling

Currently, I am dynamically loading text into a div. However, I am encountering an issue where the content below this div is constantly shifting depending on the loaded text. Is there a way to restrict the space allocated to the div with the dynamic conte ...

The combination of jQuery, using .load method in javascript to prevent scrolling up, making XMLHttpRequest requests, updating .innerHTML elements, and troubleshooting CSS/JS

While utilizing this code, CSS and Javascript are disabled (only HTML loads): function loadContent(limit) { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (xhttp.readyState == 4 && xhttp.status ...

Deactivate the form element when a user selects another button

I am facing an issue with two buttons that are linked to the same modal form. Here is the code snippet for the form: <form name="addUser" ng-submit="(addUser.$valid) ? add() : '';"> <div class="form-group has-feedback" ng-class="ad ...

Exploring nested object values in React Js through iteration

I have been searching extensively for a solution to no avail. I have an object created from fetched data with the following structure: { id: string, title: string, price: { currency: string, amount: number, }, ...