Trouble With AngularJS And Select Binding Not Loading Default Values July 24, 2022 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. T Solution 1: Two issues: Compare strings to strings Be careful when using select as and track by in the same expression. JS angular.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 }; }]); Copy HTML <!-- remove 'track by option.id' --> <select name="mySelect" id="mySelect" ng-options="option.id as option.name for option in data.availableOptions track by option.id" ng-model="data.selectedOption"> </select> Copy From 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> Copy but 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 Reference Solution 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> Copy Check 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 Sending A Canvas.toDataUrl() To Php Via AJAX I am trying to send a canvas.toDataUrl() to a php page via … How To Access Add-on Content Script In Firefox Console? I have developed an add-on for Firefox and Chrome. It has c… Arrow Key Pressed While Shift Key Is Held Down I have a Sudoku puzzle and I want to make it so the user ca… FakePath Issue In Chrome Browser I am making a browser based audio player. So for making a p… TextRange Selection MSIE I can easily set the selection on 2+ elements programatical… Set Click Event On Leaf Node i have this type of html :- Activities Physical1 … Assigning An Increment Value To Dynamic Fields I have dynamic input fields generated from a jquery functio… Angularjs Auto Prefixes Forward Slash If I hit the url say www.xyz.com/home#route-1 AngularJS au… Get And Update Text Every Few Seconds From An Http Source To An Https Website So, I have a radio station, my host provider does not have … Window.Open With PDF Stream Instead Of PDF Location Based on the question Open PDF in new browser full window, … November 2024 (32) October 2024 (52) September 2024 (19) August 2024 (208) July 2024 (189) June 2024 (409) May 2024 (687) April 2024 (422) March 2024 (793) February 2024 (904) January 2024 (781) December 2023 (809) November 2023 (341) October 2023 (517) September 2023 (275) August 2023 (335) July 2023 (277) June 2023 (371) May 2023 (206) April 2023 (127) March 2023 (137) February 2023 (182) January 2023 (243) December 2022 (121) November 2022 (237) October 2022 (172) September 2022 (165) August 2022 (505) July 2022 (300) June 2022 (284) Menu Halaman Statis Beranda © 2022 - JavaScript Square