Trouble With Angularjs And Select Binding Not Loading Default Values August 06, 2024 Post a Comment I am having trouble with a input and binding with AngluarJS. I've created a minimum working sample on Plunker: http://plnkr.co/edit/P4T25RoJrU4mvbBiUJ9S?p=preview. TSolution 1: Two issues:Compare strings to stringsBe careful when using select as and track by in the same expression.JSangular.module('defaultValueSelect', []) .controller('ExampleController', ['$scope', function($scope) { $scope.data = { availableOptions: [ {id: '1', name: 'Option A'}, {id: '2', name: 'Option B'}, {id: '3', name: 'Option C'} ], //USE this selectedOption: '2'//NOT this//selectedOption: 2 //This sets the default value }; }]); CopyHTML<!-- remove 'track by option.id' --><selectname="mySelect"id="mySelect"ng-options="option.id as option.name for option in data.availableOptions track by option.id"ng-model="data.selectedOption"></select>CopyFrom the Docs:Be careful when using select as and track by in the same expression.This will work:<select ng-options="item as item.label for item in items track by item.id" ng-model="selected"> </select> Copybut this will not work:<select ng-options="item.subItem as item.label for item in items track by item.id" ng-model="selected"> </select> Copy-- AngularJS ng-options Directive API ReferenceSolution 2: As requested by OP, adding the comment as the answer. If you remove the "track by option.id" from the ng-options as following, it should pre-select based on your model. <select name="mySelect"id="mySelect" ng-options="option.id as option.name for option in data.availableOptions" ng-model="data.selectedOption"></select> CopyCheck out the modified Plunker: http://plnkr.co/edit/b4ddyA5Q4jGNcJI1d8eW?p=preview Share Post a Comment for "Trouble With Angularjs And Select Binding Not Loading Default Values" Top Question Split Comma Separated String By Comma Need to split the string containing country names separated… Changing Font Size Options On WYSIWYG Editor? So I have a bootstrap wysiwyg editor from a jqueryscript.ne… What Is The Correct Way To "serialize" Functions In Javascript For Later Use I have a 'library' of objects that I want to load o… Bootstrap Counter Start On Display So I found this codepen tutorial that helped me get number … Select And Unselect All Checkboxes I'm not able to implement these things,In this page I w… Undo Redo For Fabric.js I'm trying to add undo/redo functionality to my Fabric.… Three.js - Scale Model With Scale.set() Or Increase Model Size? What is the best practise for scaling 3d models in Three.js… How Does Node.JS Work As Opposed To PHP? I'm thinking of switching from PHP to using Node.js for… Google Maps Api V3: Exclude Single Marker From Clustering I am using the google maps api and using grid clustering fo… Logic For The Next Button For The Questionnaire? I am beginner in AngularJS and facing some issues. I am try… December 2024 (1) November 2024 (37) October 2024 (63) September 2024 (20) August 2024 (385) July 2024 (340) June 2024 (706) May 2024 (1296) April 2024 (848) March 2024 (1553) February 2024 (1722) January 2024 (1374) December 2023 (1396) November 2023 (450) October 2023 (631) September 2023 (335) August 2023 (350) July 2023 (265) June 2023 (378) May 2023 (204) April 2023 (134) March 2023 (143) February 2023 (183) January 2023 (280) December 2022 (131) November 2022 (247) October 2022 (168) September 2022 (167) August 2022 (500) July 2022 (314) June 2022 (260) Menu Halaman Statis Beranda © 2022 - javascript edu