相思资源网 Design By www.200059.com

本文将介绍 Angular Template-driven 表单中,常用控件的使用。涉及的表单控件如下:

  1. text
  2. number
  3. radio
  4. select (基本类型)
  5. select (对象)
  6. multi select
  7. cascading select (级联操作)
  8. checkbox (boolean)
  9. multi checkbox

表单的局部效果

Angular 表单控件示例代码

数据接口

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);
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

标签:
Angular,表单控件,Angular,表单

相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com

评论“Angular 表单控件示例代码”

暂无Angular 表单控件示例代码的评论...

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。