How To Select An Option In A Dropdown List Angularjs June 06, 2024 Post a Comment I am using AngularJS directive and I need to set a selected option of a dropdown list in my template. Solution 1: The way you are doing your ng-options it will store the name of the option in scope.energy not the whole option. You were on the right track when you did:scope.energy = scope.energyOptions[0];CopyBut it won't work because it expects scope.energy to be the name and not the whole option. What you want to do in your ng-options is something like:<select id="energySource" ng-options="o as on.name for o in energyOptions" ng-model="energy" ng-selected="energy" ng-change="energyChange();"></select> CopyThe important change is the addition of the o as o.name. The 'o' on the left is what will actually be selected and stored in your scope.energy, while the as o.name is the text that will be displayed on your pull down. Solution 2: Make sure the scope.energy is outside your initialization.$scope.energyOptions = [ { name: "test1", id: "Electricity" }, { name: "test2", id: "Gas" }, { name: "test3", id: "Water" }, { name: "test4", id: "Solar" }]; $scope.energy = $scope.energyOptions[2]; Copy Share Post a Comment for "How To Select An Option In A Dropdown List Angularjs" Top Question Show/hide Div Section Based On Where The Page Is Redirected From? I have two php files which redirect like this `header('… SetInterval Slows Down With Tab/window Inactive I build a web app and I use setInterval with 500ms timer fo… Compare Two Arrays Based On Length: Skip Empty Values I have a form with 4 input (can be even many more) where th… Weird Behavior Of Long Presses In Chrome And Firefox For Android So, I found some weird behavior of long presses in Chrome a… JavaScript: How To Force Image() Not To Use The Browser Cache? If I load the nextimg URL manually in the browser, it gives… How Do Js Animations Work? Im trying to understand how to make a javascript animation … Extracting Data From Json Object In JQuery Or JS I want to use the currency data provided by https://raw.git… Show/hide Form Contents Based On Radio Selections Im building a form that has conditional fields that need to… JavaScript Selector By Class Prefix? in CSS, we can select multiple classes with same prefix lik… JQuery - Renaming Checkboxes Sets Consecutively I'm trying to build a form to allow multiple elements (… December 2024 (1) November 2024 (40) October 2024 (69) September 2024 (19) August 2024 (217) July 2024 (200) June 2024 (430) May 2024 (699) April 2024 (479) March 2024 (930) February 2024 (970) January 2024 (882) December 2023 (877) November 2023 (398) October 2023 (557) September 2023 (288) August 2023 (334) July 2023 (272) June 2023 (379) May 2023 (231) April 2023 (134) March 2023 (142) February 2023 (163) January 2023 (287) December 2022 (132) November 2022 (231) October 2022 (175) September 2022 (162) August 2022 (501) July 2022 (304) June 2022 (313) Menu Halaman Statis Beranda © 2022 - JavaScript Guide