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

时间:2016-04-20 07:01:49

标签: html angularjs twitter-bootstrap 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,因为我想在1中设置Yes,在选项中为0设置No。我该怎么办?

3 个答案:

答案 0 :(得分:1)

试试这个。在你的控制器

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

在您的视图页面

form

答案 1 :(得分:1)

你可以拥有0&amp; 1各自的状态

<强>标记

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

答案 2 :(得分:0)

您还没有提供有关该对象的更多信息,但是如果我们的对象数组是:

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

然后你可以像这样编写你的选择:

<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>