使用angularjs从选择选项组中预选选项

时间:2016-09-16 15:09:06

标签: javascript angularjs angularjs-ng-repeat ng-repeat

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>

控制器中的对象

$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"}
            ]
        }
    ];

我希望在切换它们的同时保持我的视图状态。因此,当用户选择一个选项并移动到下一个视图然后返回到前一个视图时,应该预先选择之前选择的选项。

简而言之,我想从具有选项组的选择中预先选择一个选项。

1 个答案:

答案 0 :(得分:0)

  

我们可以预先选择一个选项

     

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>
  

在控制器中:

$scope.military = 'AF - Civilian';