Ng-selected Not Working In Select Element January 03, 2023 Post a Comment I have a bound select ngModel. Therefore, once you remove ng-selected="c.CollegeName == collegeSelection.CollegeName", your code should work. I created a very simple plunk to demonstrate the "selected" feature in select directive. More details: AngularJS uses ngModel directive to enable "two-way data binding" between your model and UI elements. In the case of "select", the model collegeSelection you specified as <select ng-model="collegeSelection" ...> is the selected item. Which means if an user selects an item from the dropdown on the page, collegeSelection will be set to that item; and, if you set collegeSelection to an item in your javascript code, AngularJS will make sure that the corresponded <option> is selected. Say you have the following code in your controller: $scope.colleges = [ {id: 0, name: 'a'}, {id: 1, name: 'b'}, {id: 2, name: 'c'} ]; $scope.collegeSelection = $scope.colleges[0]; Copy And the HTML looks like: <select ng-model="collegeSelection" ng-options="c as c.name for c in colleges"></select> Copy That's it! The first college in the colleges array will be selected if you run the code. Just remember collegeSelection is the selected option, no matter it's because user selected an item on the UI, or you selected an item in javascript. That's how two-way data binding works. Solution 2: After playing around with ng-selected for a while I was not able to get it to work like you're asking. However, I was able to pre-select a specific option using ng-init. Here's a JSFiddle of my solution. My <select> ended up being: <select ng-model="selectedColor" ng-options="color.value as color.name for color in colors" ng-init="selectedColor='yellow'"> <option value="">Select A Color</option> </select>` Copy And my colors array is: colors = [ {name:'Red', value: 'red'}, {name:'Orange', value: 'orange'}, {name:'Yellow', value: 'yellow'}, {name:'Green', value: 'green'}, {name:'Blue', value: 'blue'}, {name:'Indigo', value: 'indigo'}, {name:'Violet', value: 'violet'} ] Copy Changing the ng-init="selectedColor='yellow'" to another value will select a different option. Solution 3: Some people have problems with this. I found a great solution for a simple drop down if controller as someController var vm = this; this.colors = [ {name:'Red'}, {name:'Orange'}, {name:'Yellow'}, {name:'Green'}, {name:'Blue'}, {name:'Indigo'}, {name:'Violet'} ]; this.color_selected = "Yellow"; <select ng-model="someController.color_selected" ng-options="opt.name as opt.name for opt in someController.colors"> </select> Copy ` Solution 4: I had a similar issue and realized the cause was because of the different data types. ng-model was comparing against a string value but I was pulling an integer from the database so it wasn't automatically selecting the option. To overcome this, i called toString() on the integer after querying the data from the database to ensure the data types matched. Share Post a Comment for "Ng-selected Not Working In Select Element" Top Question Javascript/HTML5: Get Current Time Of Audio Tag I have an audio tag in my template and I need to show curre… Why Does Document.activeelement Produce A Different Result On Mac Using Firefox I have the following code; When clicking the button on Lin… Why Is No File Type Returned When Adding An MKV File To A File Input? I am attempting to grab the file/mime type of a file when i… What Is Window.onpaint? Someone recommended here that window.onpaint be used to run… Array Push With JSON Can some one tell me why this prints numbers from 117 to 30… Responsive Table With Headers I've tried to render a responsive table layout so that … Dynamically Change Iframe's Content I have an iframe tag and I want to dynamically change it us… Enable/disable Radio Button Based On Another i have radio buttons actualdays_month,only working days. m… Making A Copy Of An Own Object Instead Of A Reference (updated Post, Different Q) solution I am still getting the hang of it, and its proball… Configuring Any CDN To Deliver Only One File No Matter What Url Has Been Requested I am currently working on a new project where the entire pa… November 2024 (39) October 2024 (47) September 2024 (20) August 2024 (193) July 2024 (173) June 2024 (404) May 2024 (674) April 2024 (389) March 2024 (740) February 2024 (818) January 2024 (733) December 2023 (842) November 2023 (337) October 2023 (451) September 2023 (243) August 2023 (313) July 2023 (268) June 2023 (351) May 2023 (223) April 2023 (129) March 2023 (146) February 2023 (179) January 2023 (239) December 2022 (133) November 2022 (225) October 2022 (163) September 2022 (171) August 2022 (499) July 2022 (287) June 2022 (295) Menu Halaman Statis Beranda © 2022 - JavaScript Lib