angular—demo表单控件双向数据绑定

ngModel是FormsModule中提供的指令,他其实是一个语法糖,功能等价于数据绑定+事件绑定
在这里插入图片描述

使用ngModel语法将对象的属性和表单控件的value进行双向绑定,提交的时候直接提交绑定的对象即可,不用再依次获取表单的值了

1.app.module.ts中引入FormsModule

import { FormsModule } from '@angular/forms';

@NgModule({
  declarations: [
    AppComponent,
    FormComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    FormsModule,    // 引入
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

2.创建组件

ng g component components/form

3.定义双向绑定的数据对象

form.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-form',
  templateUrl: './form.component.html',
  styleUrls: ['./form.component.css']
})
export class FormComponent implements OnInit {
  // 定义一个对象,属性对应了页面中的表单控件
  public userInfo:any={
    'username': '',    // input
    'sex': 'male',     // radio
    'cityList': ['上海','南京','北京','武汉'],   // select中的option的值
    'city': '南京',    // select最终选择的值
    'hobby': [        // checkbox
      {'title': '吃饭', 'checked': false},
      {'title': '睡觉', 'checked': false},
      {'title': '打豆豆', 'checked': false},
      {'title': '写bug', 'checked': true},
    ],
    'introduce': ''    // textarea
  }
  constructor() { }

  ngOnInit() {
  }

  // button绑定的点击事件
  onSubmit() {
    console.log(this.userInfo);
  }
}

4.页面中进行绑定

<div>
    <h1>人员登记系统</h1>
    <div class="form">
        <!-- 表单元素,双向绑定userInfo对象的属性 -->
        <li>
            姓名<input type="text" class="form-input" [(ngModel)]="userInfo.username">
        </li>

        <li>
            性别:
            <input type="radio" value='male' id="form-male" [(ngModel)]="userInfo.sex" name="sex">
            <label for="form-male"></label>
            <input type="radio" value='female' id="form-female" [(ngModel)]="userInfo.sex" name="sex">
            <label for="form-female"></label>
        </li>
        
        <li>
            地区:
            <select name="city" id="" [(ngModel)]="userInfo.city">
                <option [value]="item" *ngFor="let item of userInfo.cityList">{{item}}</option>
            </select>
        </li>

        <li>
            爱好:
            <span *ngFor="let item of userInfo.hobby;let key=index">
                <input type="checkbox" name='hobby' [id]="'check'+key" [(ngModel)]="item.checked">
                <label [for]="'check'+key">{{item.title}}</label>
            </span>
        </li>

        <li>
            简介:
            <textarea [(ngModel)]="userInfo.introduce" cols="35" rows="5"></textarea>
        </li>
        <br>
        <br>
        <button (click)="onSubmit()">提交</button>
        
    </div>
</div>

参考:https://www.bilibili.com/video/av59049211?from=search&seid=485997827014437418

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值