angular does not update dynamically created dropdowns with populated arrays

Upon loading the page, my initial dropdown functions correctly. However, the child dropdown is loaded from localstorage after the first dropdown. The issue arises when I load the second dropdown and set it to the default value - at this point, I am unable to modify its value. For reference, check out this JSFiddle: http://jsfiddle.net/silvajeff/x2nrX/3/

angular.module('demoApp', []).controller('DemoController', function($scope) {
  //my first drop down loads with the page
  $scope.options = [
     { label: 'one', value: 1 },
     { label: 'two', value: 2 },
     { label: 'three', value: 3 },
     { label: 'four', value: 4 },
     { label: 'five', value: 5 }
  ];  

  $scope.firstSelect = $scope.options[3];  

//the second drop down loads after the first makes a selection. In the real code I am populating the second from localstorage, but once it's assigned its default value I cannot change it.    
var init = function(){
   //depends on what was selected in first select
    $scope.captions = [
       { name: 'A', value: 'a' },
       { name: 'B', value: 'b' },
       { name: 'C', value: 'c' },
       { name: 'D', value: 'd' },
       { name: 'E', value: 'e' }
   ]; 
    $scope.secondSelect = $scope.captions[1];
}   
init();

});

Answer №1

To enable functionality of the second drop-down menu, please update ng-if to ng-show http://jsfiddle.net/Wc4CQ/

<body ng-app="demoApp">
    <div ng-controller="DemoController">

        <div>

            <select ng-model="firstSelect"
                ng-options="opt as opt.label for opt in options">
            </select>
<br><br>
            <select  ng-show="firstSelect.value == '4'" ng-model="secondSelect"
                ng-options="opt as opt.name for opt in captions">
            </select><br>
            The first select value is {{ firstSelect.value }}<br>
            Why can't I get the second select value to update: {{ secondSelect.value }}
        </div>

    </div>
</body>

If you prefer to use ng-if, please refer here: http://jsfiddle.net/y5Bvv/

<body ng-app="demoApp">
    <div ng-controller="DemoController">
        <div>
            <select ng-model="firstSelect" ng-options="opt as opt.label for opt in options"></select>
            <br>
            <br>
            <select ng-if="firstSelect.value == '4'" ng-model="secondSelect.select" ng-options="opt as opt.name for opt in captions"></select>
            <br>The first select value is {{ firstSelect.value }}
            <br>Why can't I get the second select value to update: {{ secondSelect.select.value }}</div>
    </div>
</body>

JS:

angular.module('demoApp', []).controller('DemoController', function ($scope) {
    //my first drop down loads with the page
    $scope.options = [{
        label: 'one',
        value: 1
    }, {
        label: 'two',
        value: 2
    }, {
        label: 'three',
        value: 3
    }, {
        label: 'four',
        value: 4
    }, {
        label: 'five',
        value: 5
    }];
    $scope.firstSelect = $scope.options[3];

    //the second drop down loads after the first makes a selection. In the real code I am populating the second from localstorage, but once it's assigned its default value I cannot change it.    
    var init = function () {
        //depends on what was selected in first select
        $scope.captions = [{
            name: 'A',
            value: 'a'
        }, {
            name: 'B',
            value: 'b'
        }, {
            name: 'C',
            value: 'c'
        }, {
            name: 'D',
            value: 'd'
        }, {
            name: 'E',
            value: 'e'
        }];
        $scope.secondSelect = {};
        $scope.secondSelect.select = $scope.captions[1];
    }
    init();

});

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

The functionality of the WordPress Contact Form 7 Plugin becomes erratic when integrated into dynamically loaded AJAX content

I am currently facing a challenge with integrating the WordPress Contact Form 7 Plugin into a website I have built on WordPress. The theme of the site utilizes jQuery to override default link behavior and AJAX to load pages without refreshing the entire pa ...

Mastering Angular: mastering the scope and utilizing ng-repeat within a Directive

Having trouble passing my "files" variable to a custom Directive: app.directive('chooseImages', function() { var files = [{name: 'a', value: 'a.jpg'}, {name: 'b', value: 'b.jpg'}, {name: 'c&ap ...

The JavaScript function is not returning the correct string when implemented in an HTML

I have written a function in a JavaScript file to return a string, and I am calling this function within a script tag inside an HTML file. function getExp() { var exp = "]]><!\\[CDATA\\["; return exp; } However, when I c ...

Move a 'square' to a different page and display it in a grid format after clicking a button

I am currently developing a project that allows students or schools to add projects and search for collaborators. On a specific page, users can input project details with a preview square next to the fields for visualization. Once the user uploads the ...

Utilize Ramda.js to transform commands into a functional programming approach

I have written a code to convert an array of numbers into a new datalist using imperative style. However, I am looking to convert it to functional style using a JavaScript library like ramdajs. Code Background: Let's say we have 5 coins in total with ...

Invoke an RxJs observable to handle errors and retry the process

I have an observable called submit$ that submits a form. If this observable encounters an error with status code 403, it means the user is not authorized and needs to log in first. Is there a way to automatically trigger another observable when a specific ...

I just made an ajax call. Now, how should I proceed with formatting the data that was returned

The ajax request below is functional, but not without its challenges. Working with HttpContext has proven to be difficult, as even Context.Response.Clear() does not seem to have any effect. How can I output only the desired content without any extra infor ...

Extract the text enclosed by two specific symbols within a string and add it to a new array

I have a string formatted as follows: var str = "-#A This text belongs to A. Dummy Text of A. -#B This text belongs to B. Dummy Text of B. -#C This text belongs to C. Dummy text of C. -#Garbage This string should be ignored" I am looking to convert this ...

When utilizing custom modules in Node.js, is it more effective to require the module from within a function or at the top of the script?

I have developed a custom script that includes all of my common settings and functions. One specific function within this script takes a timestamp as input and outputs a human-readable date with a customized format using Moment.js. The custom script is st ...

Bring in typings from a package with an alternate title

I have a project that I am currently converting to typescript. In this project, I am using the package ng-idle. Additionally, there is a corresponding @types package named angular-idle, which contains the file @types/angular-idle/index.d.ts with the follow ...

What is the best way to determine the size of the URLs for images stored in an array using JavaScript?

I am working on a project where I need to surround an image with a specific sized 'div' based on the image's dimensions. The images that will be used are stored in an array and I need to extract the height and width of each image before disp ...

In JavaScript, a "switch statement" retrieves a test value from a "input type text" by accessing the value using getElementByName.value

Attempting to test 7 possible values for the variable 'a' by passing it to a function from user input in seven 'input type text' boxes, then checking each value individually with clicks on 7 'input type image' elements. Howeve ...

Error with compiling due to passing an array as a parameter in Visual C++ 2010

I'm currently in the early stages of learning programming, and I've recently been introduced to QuickSort algorithms. In my quest to understand this concept better, I attempted to pass an integer array as a parameter to a function called quickSor ...

Dynamic data retrieval with the power of JavaScript and AJAX

When attempting to send data using AJAX in PHP, I encountered an issue with my jQuery click function on a button that sends data only when the quantity is greater than 1. The error arises because PHP does not recognize the variables 'name' and &a ...

Is there a way to append parameters to the end of each state query in UI Router for Angular version 1.6?

Need help with an Angular 1.6 application that uses UI-router and has multiple states? Want to add parameters to the end of all state URLs? Here's how you can do it: $stateProvider .state('something') url: '/' .st ...

Having trouble loading my script in the frontend plugin?

After much testing, I have discovered that when I place this code before the get_header() function, the script is successfully loaded. However, when I move it after the get_header() function, it no longer works as intended. It's important to note that ...

Pair two arrays based on a specific key value

I'm currently facing a challenge where I need to associate two arrays of objects, and I could really use some help with this. Explanation : One array contains various reference products const array1 = [ {name: peanuts, referenceKey: 0}, {name: almon ...

What are the steps to fix the "Invariant Violation" issue that is linked to the redux store?

During my DOM testing to verify if a dialog box would open upon clicking a button, I encountered an error message: Invariant Violation: Could not find "store" in either the context or props of >"Connect(Photos)". Either wrap the root component in a , ...

Issue with Backbone collection not being updated despite making a JSONP request

Recently, I delved into the world of Backbone.js and currently, I am immersed in developing an app using Brunch that makes a JSONP request to an external API for populating my collection and models. Despite following guidance from previous posts (here and ...

Attempting to include a standard VAT rate of 5% on the invoice

/* The code format is correct and I don't see any issues on my end, I hope it works well for you. */ I need assistance in adding a fixed VAT (tax) rate of 5%. The tax amount should be displayed on the row, while the total tax should reflect the sum o ...