本文将介绍 Angular Template-driven 表单中,常用控件的使用。涉及的表单控件如下:
- text
- number
- radio
- select (基本类型)
- select (对象)
- multi select
- cascading select (级联操作)
- checkbox (boolean)
- multi checkbox
表单的局部效果
数据接口
export interface User { name: string; // 名字 age"htmlcode"><div class="form-group"> <label>名字</label> <input type="text" class="form-control" name="name" [(ngModel)]="user.name"> </div>Number
<div class="form-group"> <label>年龄</label> <input type="number" class="form-control" name="age" [(ngModel)]="user.age"> </div>Radio
数据
genders = [{ value: 'F', display: '女' },{ value: 'M', display: '男' }];模板
<div class="form-group"> <label>性别</label> <label *ngFor="let gender of genders;"> <input type="radio" name="gender" [(ngModel)]="user.gender" [value]="gender.value"> {{gender.display}} </label> </div>Select - 基本类型
数据
复制代码 代码如下:
roles = [{ value: 'admin', display: '管理员' },{ value: 'user', display: '普通用户' }];
模板
<div class="form-group"> <label>角色</label> <select name="role" class="form-control" [(ngModel)]="user.role"> <option *ngFor="let role of roles" [value]="role.value"> {{role.display}} </option> </select> </div>Select - 对象
数据
themes: Theme[] = [ { backgroundColor: 'black', fontColor: 'white', display: '黑色' }, { backgroundColor: 'white', fontColor: 'black', display: '白色' }, { backgroundColor: 'grey', fontColor: 'white', display: '灰色' } ];模板
<div class="form-group"> <label>主题</label> <select name="theme" class="form-control" [(ngModel)]="user.theme"> <option *ngFor="let theme of themes" [ngValue]="theme"> {{theme.display}} </option> </select> </div>Mulit Select
数据
topics = [ { value: 'game', display: '游戏' }, { value: 'tech', display: '科技' }, { value: 'life', display: '生活' } ];模板
<div class="form-group"> <label>话题</label> <select multiple name="topics" class="form-control" [(ngModel)]="user.topics"> <option *ngFor="let topic of topics" [value]="topic.value"> {{topic.display}} </option> </select> </div>Cascading Select - 级联操作
数据
provinces = [ {pk:1,pv:'北京'}, {pk:16,pv:'福建'} ]; citieData = [ {pk:1,ck:72,cv:'朝阳区'},{pk:1,ck:2800,cv:'海淀区'}, {pk:1,ck:2801,cv:'西城区'},{pk: 16,ck: 1303,cv: '福州市'}, {pk: 16,ck: 1315,cv: '厦门市'},{pk: 16,ck: 1332,cv: '泉州市'} ]; cities = this.citieData.filter(city => city.pk === 16); // 默认福建省模板
<div class="form-group"> <label>地址</label> <select name="province" class="form-control" [(ngModel)]="user.province" (change)="changeProvince($event.target.value)"> <option *ngFor="let province of provinces" [value]="province.pk"> {{province.pv}} </option> </select> <select name="city" class="form-control" [(ngModel)]="user.city"> <option *ngFor="let city of cities" [value]="city.ck"> {{city.cv}} </option> </select> </div>Checkbox
模板
<div class="form-group"> <div class="checkbox"> <label> <input type="checkbox" name="isActive" [(ngModel)]="user.isActive"> 是否激活 </label> </div> </div>Multi Checkbox
数据
hobbies = [ { value: 'reading', display: '看书' }, { value: 'music', display: '听歌' }, { value: 'movie', display: '电影' } ];模板
<div class="form-group"> <label>爱好</label> <span *ngFor="let hobby of hobbies"> <input type="checkbox" name="bobby" [value]="hobby.value" [checked]="user.hobbies[hobby.value]" (change)="changeHobby(hobby, $event)" /> {{hobby.display}} </span> </div>完整示例
模板
<h4>{{title}}</h4> <form #f="ngForm" novalidate> <div class="form-group"> <label>名字</label> <input type="text" class="form-control" name="name" [(ngModel)]="user.name"> </div> <div class="form-group"> <label>年龄</label> <input type="number" class="form-control" name="age" [(ngModel)]="user.age"> </div> <div class="form-group"> <label>性别</label> <label *ngFor="let gender of genders;"> <input type="radio" name="gender" [(ngModel)]="user.gender" [value]="gender.value"> {{gender.display}} </label> </div> <div class="form-group"> <label>角色</label> <select name="role" class="form-control" [(ngModel)]="user.role"> <option *ngFor="let role of roles" [value]="role.value"> {{role.display}}</option> </select> </div> <div class="form-group"> <label>主题</label> <select name="theme" class="form-control" [(ngModel)]="user.theme"> <option *ngFor="let theme of themes" [ngValue]="theme"> {{theme.display}} </option> </select> </div> <div class="form-group"> <div class="checkbox"> <label> <input type="checkbox" name="isActive" [(ngModel)]="user.isActive"> 是否激活 </label> </div> </div> <div class="form-group"> <label>话题</label> <select multiple name="topics" class="form-control" [(ngModel)]="user.topics"> <option *ngFor="let topic of topics" [value]="topic.value"> {{topic.display}} </option> </select> </div> <div class="form-group"> <label>爱好</label> <span *ngFor="let hobby of hobbies"> <input type="checkbox" name="bobby" [value]="hobby.value" [checked]="user.hobbies[hobby.value]" (change)="changeHobby(hobby, $event)" /> {{hobby.display}} </span> </div> <div class="form-group"> <label>地址</label> <select name="province" class="form-control" [(ngModel)]="user.province" (change)="changeProvince($event.target.value)"> <option *ngFor="let province of provinces" [value]="province.pk"> {{province.pv}} </option> </select> <select name="city" class="form-control" [(ngModel)]="user.city"> <option *ngFor="let city of cities" [value]="city.ck">{{city.cv}}</option> </select> </div> <button type="submit" (click)="save()" class="btn btn-default">保存</button> </form>组件类
export class AppComponent implements OnInit { title = '新增用户'; user: User; genders = [{ value: 'F', display: '女' },{ value: 'M', display: '男' }]; roles = [{ value: 'admin', display: '管理员' },{ value: 'user', display: '普通用户' }]; themes: Theme[] = [ { backgroundColor: 'black', fontColor: 'white', display: '黑色' }, { backgroundColor: 'white', fontColor: 'black', display: '白色' }, { backgroundColor: 'grey', fontColor: 'white', display: '灰色' } ]; topics = [ { value: 'game', display: '游戏' }, { value: 'tech', display: '科技' }, { value: 'life', display: '生活' } ]; hobbies = [ { value: 'reading', display: '看书' }, { value: 'music', display: '听歌' }, { value: 'movie', display: '电影' } ]; provinces = [ {pk:1,pv:'北京'}, {pk:16,pv:'福建'} ]; citieData = [ {pk:1,ck:72,cv:'朝阳区'},{pk:1,ck:2800,cv:'海淀区'}, {pk:1,ck:2801,cv:'西城区'},{pk: 16,ck: 1303,cv: '福州市'}, {pk: 16,ck: 1315,cv: '厦门市'},{pk: 16,ck: 1332,cv: '泉州市'} ]; cities = this.citieData.filter(city => city.pk === 16); changeHobby(hobby, event) { this.user.hobbies[hobby.value] = event.target.checked; } changeProvince(pk) { this.cities = this.citieData.filter((city)=> city.pk == pk); this.user.city = this.cities[0].ck; } ngOnInit(): void { this.user = { name: '', gender: this.genders[0].value, role: this.roles[1].value, theme: this.themes[0], isActive: false, hobbies: {'music': true}, topics: [this.topics[1].value], province: 16, // 福建省 city: 1315 // 厦门市 } } save() { console.log(this.user); } }以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
狼山资源网 Copyright www.pvsay.com
暂无“Angular 表单控件示例代码”评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。