简体   繁体   中英

Preselect an option from select option groups using angularjs

HTML:

<select id="military" name="military" ng-model="military" ng-change="militaryChange()" style="color: #000;">
    <optgroup label="{{value.label}}" ng-repeat="(key,value) in militaryObject">
        <option value="{{v.value}}" ng-repeat="(k,v) in value.options">{{v.name}}</option>
    </optgroup>
</select>

Object in controller

$scope.militaryObject = [
        {
            label: "U.S. Air Force",
            options: [
                {"name": "Active Duty", "value": "AF - Active Duty (AD)"},
                {"name": "Selective Reserve", "value": "AF - Selective Reserve (SR)"},
                {"name": "Spouse", "value": "AF - Spouse of AD or SR"},
                {"name": "Veteran", "value": "AF - Veteran"},
                {"name": "Civilian", "value": "AF - Civilian"},
                {"name": "Air National Guard", "value": "AF - Air National Guard"}
            ]
        },{
            label: "U.S. Army",
            options: [
                {"name": "Active Duty", "value": "AR - Active Duty (AD)"},
                {"name": "Selective Reserve", "value": "AR - Selective Reserve (SR)"},
                {"name": "Spouse", "value": "AR - Spouse of AD or SR"},
                {"name": "Veteran", "value": "AR - Veteran"},
                {"name": "Civilian", "value": "AR - Civilian"},
                {"name": "Army National Guard", "value": "Army - Air National Guard"}
            ]
        },{
            label: "U.S. Coast Guard",
            options: [
                {"name": "Active Duty", "value": "CG - Active Duty (AD)"},
                {"name": "Selective Reserve", "value": "CG - Selective Reserve (SR)"},
                {"name": "Spouse", "value": "CG - Spouse of AD or SR"},
                {"name": "Veteran", "value": "CG - Veteran"},
                {"name": "Civilian", "value": "CG - Civilian"}
            ]
        },{
            label: "U.S. Marine Corps",
            options: [
                {"name": "Active Duty", "value": "MC - Active Duty (AD)"},
                {"name": "Selective Reserve", "value": "MC - Selective Reserve (SR)"},
                {"name": "Spouse", "value": "MC - Spouse of AD or SR"},
                {"name": "Veteran", "value": "MC - Veteran"},
                {"name": "Civilian", "value": "MC - Civilian"}
            ]
        },{
            label: "U.S. Navy",
            options: [
                {"name": "Active Duty", "value": "NV - Active Duty (AD)"},
                {"name": "Selective Reserve", "value": "NV - Selective Reserve (SR)"},
                {"name": "Spouse", "value": "NV - Spouse of AD or SR"},
                {"name": "Veteran", "value": "NV - Veteran"},
                {"name": "Civilian", "value": "NV - Civilian"}
            ]
        },{
            label: "U.S. Department of Defense",
            options: [
                {"name": "DoD Civilian", "value": "DoD - Civilian"}
            ]
        }
    ];

I want to maintain the state of my view while switching between them. So when user selects an option and moves to the next view and then come back to a previous one, so the option he selected previously should be pre selected.

In short I want to pre-select an option from a select that has option groups.

We can pre-select an option

HTML:

<select id="military" name="military" ng-model="military" ng-change="militaryChange()" style="color: #000;">
    <optgroup label="{{value.label}}" ng-repeat="(key,value) in militaryObject">
        <option value="{{v.value}}" ng-repeat="v in value.options">{{v.name}}</option>
    </optgroup>
</select>

In controller:

$scope.military = 'AF - Civilian';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM