When the value of the second dropdown is selected, the first dropdown in Angular will repopulate

I am currently facing an issue with a page that contains two dropdown menus. The first dropdown menu displays a list of Categories, while the second dropdown menu dynamically populates a list of Services based on the selected Category.

Although the first dropdown menu is populating correctly and the associated services are being displayed in the second dropdown, I am encountering a problem. When I select a service from the second dropdown, the Category list resets and loses the previously selected value.

For example, if I choose "Account" from the Category list, the services dropdown correctly shows two options. However, when I then select a service, the Category list reverts back to its original state upon page load.

Below is the HTML code:

 <select data-ng-model="vm.kioskCheckin.serviceId" data-ng-options="c.Name as c.Name for c in vm.categories"
                ng-change="vm.getServicesBC()"
                class="form-control input-lg" ng-required="true"
                id="category">
          <option value="">Choose Primary Reason for Visit</option>
        </select>
        <div class="v-Space10"></div>
        <select id="servicesDropDown" data-ng-model="vm.kioskCheckin.serviceId"
                data-ng-options="c.Name as c.Name for c in vm.servicesFiltered"
                ng-change="vm.setValues()"
                class="form-control input-lg invisible" ng-required="true">
          <option value="">Select Service...</option>
        </select>

Additionally, here is the function that populates the second dropdown (note: the second dropdown is hidden until a category is selected in the first dropdown):

 vm.getServicesBC = function () {
  var mySelectedCategory = $('#category').val().split("string:").pop();
  vm.servicesFiltered = getServicesByCategory(mySelectedCategory);
    vm.filteredServices = getServicesByCategory(vm.services);
    vm.filteredServices = [];
    $('#servicesDropDown').removeClass("invisible");
  };

If you have any suggestions or pointers on what may be causing this issue, please let me know. Thank you!

Answer №1

Upon reflection, I realized that using the same model was causing issues with overwriting and repopulating the Category drop down.

Check out the updated HTML below:

<div class="text-center" style="margin: 25px 0 50px;">
        <select data-ng-model="vm.selectedCategory" data-ng-options="c.Name as c.Name for c in vm.categories"
                ng-change="vm.getServicesBC()"
                class="form-control input-lg" ng-required="true"
                id="category">
          <option value="">Choose Primary Reason for Visit</option>
        </select>
        <div class="v-Space10"></div>
        <select id="servicesDropDown" data-ng-model="vm.kioskCheckin.serviceId"
                data-ng-options="s.ServiceId as s.DisplayName for s in vm.filteredServices"
                class="form-control input-lg invisible" ng-required="true">
          <option value="">Select Service...</option>
        </select>

Furthermore, here is the updated javascript code:

vm.getServicesBC = function () {
  vm.filteredServices = getServicesByCategory(vm.selectedCategory);
    $('#servicesDropDown').removeClass("invisible");
  };

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

Best practices for ensuring text remains in the correct position on all screen sizes when overlaying a large image

In the introductory page, I have a large image that spans more than 4000px in width to accommodate different resolutions. The image is set with the following CSS: #source-image { width: 100%; position: absolute; top: 0; left: 0; } On top ...

Utilizing JavaScript to sift through JSON data

Looking to efficiently parse a large JSON file based on its values. This is an excerpt from my extensive JSON code (with over 1000 entries). var jsonObject = [ { "UserId":10259, "FullName":"hello world", "CustomerId":"10165" ...

How can I incorporate fetch into my code using VSCode?

I am completely new to using JS. I want to incorporate fetch in VSCode, but I'm having trouble importing it. When I try: import fetch from "node-fetch"; I get this error: (node:19856) Warning: To load an ES module, set "type": &q ...

tracking the number of new buttons pressed (HTML/Javascript)

As a novice in programming, I am currently tackling a task involving a table in otree that displays 256 unique buttons using Javascript. Each button reveals an icon when clicked, thanks to a straightforward function. Within this function, I have incorpora ...

Tips for iterating through an array of objects nested within other objects

Good afternoon everyone, I’m having trouble accessing the attributes of a JavaScript object array through AJAX. Can anyone spot where I might be going wrong? View image description This is my AJAX call: $("#cbx1").on("change", func ...

Unable to assign a value to an indexed property in 'FileList': Setter function for index properties is not enabled

angular.module('myApp') .directive('fileModel', ['$parse', 'uploadService','messageService','CONF', function ($parse, uploadService,messageService,CONF) { return { restrict ...

Customize the <td> column based on values and rows. Modify the current jQuery code

This code snippet contains an HTML table that dynamically populates based on the dropdown selection. The script included in the code highlights the best and worst values in the table by changing their background color to green and red, respectively. & ...

Is it possible for Angular to either remove my object from the scope or combine it with my provider during execution?

I'm trying to figure out how to handle an object called xyzApi in my code. This object is defined outside of my angular code and contains classes and utility methods that I want to make accessible to the rest of my API. This is what it looks like (in ...

Building an object in React using JavaScript: A step-by-step guide to including all necessary fields

I am working on a Sign-up page where I console log the input values from each field when the 'Sign Up' button is clicked. However, I want to combine these individual values into one object in the console. If anyone can provide assistance with thi ...

Utilizing Angular PrimeNG's range datepicker, you can select a date range spanning from January 31st to December 1st, 2023 within a reactive form. Take it a step further by calculating

Here is some HTML code: <div class="row"> <div class="col-4"> <p-calendar label="startDate" formControlName="startDate" [minDate]="daMaxRange" ...

Avoid updating the input from ng-model while it is being edited

There are times when my model.field can be altered by both user input into an input field and by other functions running in the background. However, I want to handle situations where changes made by the user take precedence over modifications made by those ...

`Generating an ever-changing masonry layout on a website using live data`

I'm currently working on a new web project and I want to incorporate a masonry view for the images on the homepage. The plan is to host this project on Azure, using blob storage for all the images. My idea is to organize the images for the masonry vi ...

How should one go about editing the vertices of a box geometry?

I have been customizing the vertices of various box geometries to create unique shapes or adjust their heights. Everything looks great in my scenes (check out this example https://i.sstatic.net/w7Z3G.jpg). However, I encountered an issue when using the Ob ...

Error encountered: Required closing JSX tag for <CCol> was missing

Encountered a strange bug in vscode...while developing an app with react.js. Initially, the tags are displaying correctly in the code file. However, upon saving, the code format changes causing errors during runtime. For instance, here is an example of the ...

Previous Button on jQuery/Javascript Slider Not Functioning

Having a small issue with my previous button, #prev, as it is not scrolling back to the previous image but instead to the next image. This is the script I am using: http://jquery.malsup.com/cycle/int2.html Additionally, when I try to set the width of the ...

Easy Ways to Retrieve the Current Date and Time

Is there a way to retrieve the current date and time using angularjs I attempted the following method: <b>{{Date.Now}}</b> Unfortunately, this approach did not yield the desired results. Any tips or suggestions would be highly valued. ...

Can you walk me through how async/await works?

Can someone provide a detailed explanation of this code? const promiseFactory = () => new Promise(resolve => setTimeout(() => resolve(1), 5000)); If I execute the following: const consumer = async() => { promiseFactory().then(s => con ...

Guide on using a pipe feature with varying arguments when the main function is called

I am delving into functional programming for the first time and have a query regarding using pipes. Imagine I have this particular function: const updateArray = R.curry((index, value, array) => Object.assign([], array, {[index]: v ...

trouble with phonegap javascript ajax integration

I'm new to app development and I've been trying to create a mobile app using PhoneGap. I have a remote shared server that contains a MySQL table. My goal is to sign up a user, then send the data via JavaScript and AJAX to a PHP server page that w ...

Deleting an element from a JavaScript array

I have a collection of javascript functions that manage intervals by setting and clearing them. The intervals are stored in an array called intervals[]. config: { settings: { timezone: 'Australia/Perth,', base_url: location.p ...