没有以角度2检查单选按钮

时间:2017-03-03 06:25:01

标签: javascript jquery angular

我为性别创建了两个单选按钮,我想显示之前选中的单选按钮。

我在模板

中添加了以下代码
<div class="form-group">
    <label>Gender</label>

    <div id="input-type">
        <label class="radio-inline">
            <input type="radio" formControlName="gender" [name]="'gender'" [value]="'male'"/> Male
        </label>
        <label class="radio-inline">
              <input type="radio" formControlName="gender" [name]="'gender'" [value]="'female'"/> Female
        </label>
    </div>
</div>

我在Component中添加了以下代码。

console.log(this.gender);  // I am getting 'male' here.
this.editProfile = new FormGroup({
     gender: new FormControl(this.gender || null)
});

表格的其他值正在显示,但未检查单选按钮。

3 个答案:

答案 0 :(得分:1)

你可以像

那样写
def my_game():
    question = input("Press 'r' for rock, 'p' for paper or 's' for scissors")
    if question is 'r':
        print('I choose paper, I win!')
    elif question is 's':
        print('I choose rock, I win!')
    elif question is 'p':
        print('I choose scissors, I win!')

    for restart in question:
        print(restart) # this command will print the string entered by the user, one character in each line

while True:
    my_game()

它应该适合你

找到PLUNK here

答案 1 :(得分:1)

我找到了其他解决方案。我们可以使用下面的代码

<div class="form-group">
    <label>Gender</label>
      <div id="input-type">
          <label class="radio-inline">
              <input type="radio" [checked]="editProfile.value.gender == 'male'" formControlName="gender" value="male"/> Male
          </label>
          <label class="radio-inline">
                <input type="radio" [checked]="editProfile.value.gender == 'female'" formControlName="gender" value="female"/> Female
          </label>
      </div>
</div>

答案 2 :(得分:0)

当您使用Reactive Forms时,您可以在输入中省略name属性,因为您要将字符串值设置为输入,因此您必须使用value="male"而不是[value]="'male'"

<div class="form-group">
  <label>Gender</label>

  <div id="input-type">
    <label class="radio-inline">
        <input type="radio" formControlName="gender" value="male"/> Male
    </label>
    <label class="radio-inline">
          <input type="radio" formControlName="gender" value="female"/> Female
    </label>
  </div>
</div>