如何在angularJS中使用django选项

时间:2016-07-03 13:54:50

标签: angularjs django

我在用户模型中有选择字段:

GENDER_CHOICES = (
     ('M', 'Male'),
     ('F', 'Female'),
 )

gender = models.CharField(max_length=1, choices=GENDER_CHOICES, default='M')

序列化之后我得到: enter image description here

在我的模板中我想使用单选按钮:

<div class="radio-item">
  <div class="label-text">Пол</div>
  <div class="item">
    <input type="radio" name="sex" id="male">
    <label for="male"><span></span> Male</label>
  </div>
  <div class="item">
     <input type="radio" name="sex" id="female">
     <label for="female"><span></span> Female</label>
   </div>
</div>

1 个答案:

答案 0 :(得分:1)

您似乎从API获取数据并绑定到表单。因此,在您的情况下,您可以ng-model使用radio输入。

但在第一件事之前,你需要将整个对象绑定到$scope.user = data;。另外我假设无线电值是M&amp; F

<强> HTML

<div class="radio-item">
  <div class="label-text">Пол</div>
  <div class="item">
    <input type="radio" name="sex" id="male" ng-model="user.gender" value="M">
    <label for="male"><span></span> Male</label>
  </div>
  <div class="item">
     <input type="radio" name="sex" id="female" ng-model="user.gender" value="F">
     <label for="female"><span></span> Female</label>
   </div>
</div>