AngularJS: 为 ng-options 中的选项设置值

AngularJS: Set value for options in ng-options

我有一个名为 IsMarried
的客户的布尔值 属性 我想使用 <select> 标签如下:

<select name="isMarried" id="isMarried" class="form-control" ng-model="customer.isMarried" ng-options="statue for statue in maritalStatues"></select>

我应该如何更改 ng-options,因为我想在选项中为 Yes 设置 1,为 No 设置 0。我该怎么办?

您没有提供太多关于对象的信息,但如果我们的对象数组是这样的:

$scope.maritialStatus = [ { id : 1, name : "Married"}, {id:2,name: "Unmarried"}]

然后你可以这样写你的select:

<label for="singleSelect"> Single select: </label><br>
    <select name="repeatSelect" id="repeatSelect" ng-model="customer.isMarried">
      <option ng-repeat="status in martialStatus" value="{{status.id}}">{{status.name}}</option>
    </select>

试试这个。在你的控制器中

$scope.maritalStatues = [{statusKey: 0 , statusTitle : 'Unmarried'},{statusKey: 1 , statusTitle : 'Married'}];

在您的查看页面上

 <select name="isMarried" id="isMarried" class="form-control" ng-model="customer.isMarried" ng-options="status.statusKey as status.statusTitle for status in  maritalStatues">
                </select>

你可以有 01 各自的状态

标记

<select name="isMarried" id="isMarried" 
   class="form-control" ng-model="customer.isMarried" 
   ng-options="statue.value as statue.description for statue in maritalStatues">
</select>

代码

$scope.maritialStatus = [ { value : 1, description : "Yes"}, {value : 0, description : "Unmarried"}];