While ng-model is used in select box and ng-selected is also used in options with some condition that time ng-selected is not working.
If I will remove ng-model than ng-selected is working, but if i will remove ng-model than how I should get the value of select box in controller.
Please help !
Here is my code...
HTML:
<select class="form-control" ng-change="accessModeSelected()">
<option ng-selected="mode.status == '1'" ng-repeat="mode in storageResult.accessMode" ng-value="mode.name" name="accessMode{{$index}}" id="accessMode">
{{mode.name}}
</option>
</select>
AngularJS:
$scope.storageResult = {
"storageAccount":"Enable",
"user": "sdcard",
"pass": "sdcard",
"wifiIP": "0",
"ipAddr": "0",
"accessMode": [
{"name": "Local Storage","status": "0"},
{"name":"WiFi Storage", "status": "1"},
{"name":"Internet Storage", "status": "0"}
]
}
Use ng-options
and ng-init
(for setting default value) instead of ng-repeat
.
ng-options
is specifically for select
<select class="form-control" ng-init="statusselect='WiFi Storage'" ng-model="statusselect" ng-options="mode.name as mode.name for mode in storageResult.accessMode">
</select> Selected Value : {{statusselect}}
Edit: Using ng-repeat
I would prefer ng-options
,but if you want to use ng-selected
with ng-repeat
you'll need provide a default selected value to ng-model
from your controller
<select class="form-control" ng-model="statusselect">
<option ng-selected="{{mode.name == statusselect}}" ng-repeat="mode in storageResult.accessMode" value="{{mode.name}}" name="accessMode{{$index}}" id="accessMode">
{{mode.name}}
</option>
</select> Selected Value : {{statusselect}}
Inside Controller
$scope.storageResult = {
"storageAccount":"Enable",
"user": "sdcard",
"pass": "sdcard",
"wifiIP": "0",
"ipAddr": "0",
"accessMode": [
{"name": "Local Storage","status": "0"},
{"name":"WiFi Storage", "status": "1"},
{"name":"Internet Storage", "status": "0"}
]
}
$scope.statusselect = $scope.storageResult["accessMode"][1]["name"];