How To Select An Option In A Dropdown List Angularjs September 08, 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"
Post a Comment for "How To Select An Option In A Dropdown List Angularjs"