Angularjs 在ng选项中设置自定义id

Angularjs 在ng选项中设置自定义id,angularjs,ng-options,Angularjs,Ng Options,我正在使用Angular来构建一个使用ng选项的SELECT,并且所有的功能都在发挥作用,除了我想设置值,而不是让Angular自动填充这个值 $scope.form.stage = [{ optional: 1 id: 23 description: UD Visual Inspection - optional },{ optional: 0 id: 12 description: Flash

我正在使用Angular来构建一个使用ng选项的SELECT,并且所有的功能都在发挥作用,除了我想设置值,而不是让Angular自动填充这个值

$scope.form.stage = [{ 
       optional: 1
       id: 23
       description: UD Visual Inspection - optional
     },{ 
       optional: 0
       id: 12
       description: Flash
     }]
我的指示:

生成的HTML

})

从工厂返回的数据

如果将selectedStage设置为整个选定对象,则可以从form.selectedStage.id获取所需的值


我认为您需要使用track by语法:ng options='item.description for item in form.stage track by id'非常清楚地解释了如何做到这一点,select as…'track by id'只呈现一个选项框第一个选项框的值为'?'也尝试了这一点,但运气不佳-ng options='select as description for id,form.stage中的说明'item.id as item.description for item in form.stage表示到目前为止,plunker绝对有用。我最初是这样做的,但selectedStage可能会出现JSON格式错误的问题,请参见上面的更新。当我选择一个阶段时,selectedStage模型在其中获得$ref,而不是值,同样当我检查元素时,我没有看到我的ID的值。不确定我在执行此任务时出错的地方。无需担心,进行了此调整,它似乎正在工作:ng options='item.id as item.description for item in form.form.stage'
<select name='test_stage' ng-model='form.selectedStage' ng-change='abc(form)' ng-options='item.description for item in form.stage'>
<select name="test_stage" id="test_form_stage" tabindex="2" ng-model="form.selectedStage" ng-change="abc(form)" ng-options="item.description for item in form.stage" ng-blur="fetchTestStation(form)" class="ng-valid ng-dirty"><option value="0">UD Visual Inspection - optional</option><option value="1">Flash</option></select>
<select name="test_stage" id="test_form_stage" tabindex="2" ng-model="form.selectedStage" ng-change="abc(form)" ng-options="item.id as item.description for item in form.stage" ng-blur="fetchTestStation(form)" class="ng-valid ng-dirty"><option value="0">UD Visual Inspection - optional</option><option value="1">Flash</option></select>
form:  { 
    selectedStage: null
    stage: 
 [{ 
    optional: 1
    id: 23
    description: UD Visual Inspection - optional
 },{ 
    optional: 0
    id: 13
    description: Engraving
 }]
} 
    selectedStage:  { 
        $ref: $["form"]["stage"][1]
} 
<select name="test_stage" id="test_form_stage" tabindex="2" ng-model="form.selectedStage" ng-options="item as item.description for item in form.form.stage" ng-blur="fetchTestStation(form)" class="ng-valid ng-dirty"><option value="?"></option><option value="0">UD Visual Inspection - optional</option><option value="1">Engraving</option></select>
    // Set vars
    $scope.workorder = {};
    $scope.product = {};

    // Set params to send in request
    var params = $.param({
        serial: $scope.serial
    });

    /**
     * Get stage data for serial from API [Factory]
     */
    tstFrmServices.locateRecord(params).success(function (result) {
        $scope.data = result.data;

        // Handle successfull response
        if ($scope.data['success'][0].code == 200){

             $scope.form = {
                selectedStage: null,
                stage: $scope.data['success'][0].data
              };

            $scope.workorder = $scope.data['success'][0].wo;
            $scope.product = $scope.data['success'][0].product;
        }


    }).error(function (result) {

    });
};
"data": [{"optional": 1, "id": 23, "description": "UD Visual Inspection - optional"}, {"optional": 0, "id": 13, "description": "Engraving"}], "product": "10P91685-010"}]}
<select name='test_stage' 
  ng-model='form.selectedStage' 
  ng-change='abc(form)' 
  ng-options='item as item.description for item in form.stage'>
</select>