• angular2单选按钮示例


    注意事项:

      单选按钮表单里 fromControlName 和name 的值必须相同

    1.导入

    import { ReactiveFormsModule } from '@angular/forms'
     
    2.html模板
     
    <form [formGroup]="fg">
        <label>性别:</label>
        <input type="radio" id="male" name="sex" value='1' formControlName="sex">
        <label for="male">男</label>
        <input type="radio" id="female" name="sex"  value='2' formControlName="sex">
        <label for="female">女</label>
        <button (click)="run()">跳转</button>
    </form>
     
    3. ts文件
    import { Component, OnInit } from '@angular/core';
    import { FormGroup, FormControl } from '@angular/forms';

    @Component({
      selector: 'app-list',
      templateUrl: './list.component.html',
      styleUrls: ['./list.component.css']
    })
    export class ListComponent implements OnInit {

      public value:string='2';
      constructor() { }

      ngOnInit() {
        this.backGoup();
      }
      
      public fg:FormGroup;
      backGoup(){
         this.fg=new FormGroup(
          {
            sex:new FormControl(this.value)
          }
        );
      }
      run(){
      //TypeScript里的解构形式 
      //未取出key 为sex的value值
        let {sex}=this.fg.value;
        console.log(sex);
      }

    }
     
  • 相关阅读:
    MySQL权限详解
    MySql 详解
    顶级Python库
    第一次读到就震撼的句子
    Windows快捷键大全
    Pycharm超级好用的快捷键——效率之王
    Django框架
    前端入门和进阶必会
    正则表达式BREs,EREs,PREs的比较
    selenium模块基础用法详解
  • 原文地址:https://www.cnblogs.com/kukai/p/12142433.html
Copyright © 2020-2023  润新知