本文将介绍 Angular Template-driven 表单中,常用控件的使用。涉及的表单控件如下:
- text
- number
- radio
- select (基本类型)
- select (对象)
- multi select
- cascading select (级联操作)
- checkBox (boolean)
- multi checkBox
表单的局部效果
数据接口
rush:js;">
export interface User {
name: string; // 名字
age?: number; // 年龄
gender?: string; // 性别
role?: string; // 角色
theme?: Theme; // 主题
isActive?: boolean; // 是否激活
hobbies?: {[key: string]: boolean}; // 爱好
topics?: string[]; // 话题
province: number; // 省
city: number; // 市
}
export interface Theme {
display: string; // 显示文本
backgroundColor: string; // 背景颜色
fontColor: string; // 字体颜色
}
控件使用示例
Text
rush:xhtml;">
名字
Number
rush:xhtml;">
年龄
Radio
数据
模板
rush:xhtml;">
性别
Select - 基本类型
数据
代码如下:
模板
rush:xhtml;">
角色
Select - 对象
数据
rush:js;">
themes: Theme[] = [
{ backgroundColor: 'black',fontColor: 'white',display: '黑色' },{ backgroundColor: 'white',fontColor: 'black',display: '白色' },{ backgroundColor: 'grey',display: '灰色' }
];
模板
rush:xhtml;">
Mulit Select
数据
rush:js;">
topics = [
{ value: 'game',display: '游戏' },{ value: 'tech',display: '科技' },{ value: 'life',display: '生活' }
];
模板
rush:xhtml;">
话题
Cascading Select - 级联操作
数据
citieData = [
{pk:1,ck:72,cv:'朝阳区'},{pk:1,ck:2800,cv:'海淀区'},ck:2801,cv:'西城区'},{pk: 16,ck: 1303,cv: '福州市'},ck: 1315,cv: '厦门市'},ck: 1332,cv: '泉州市'}
];
{pk:1,ck:72,cv:'朝阳区'},{pk:1,ck:2800,cv:'海淀区'},ck:2801,cv:'西城区'},{pk: 16,ck: 1303,cv: '福州市'},ck: 1315,cv: '厦门市'},ck: 1332,cv: '泉州市'}
];
cities = this.citieData.filter(city => city.pk === 16); // 默认福建省
模板
rush:xhtml;">
地址
CheckBox
模板
rush:xhtml;">
Box">
Multi CheckBox
数据
rush:js;">
hobbies = [
{ value: 'reading',display: '看书' },{ value: 'music',display: '听歌' },{ value: 'movie',display: '电影' }
];
模板
rush:xhtml;">
爱好
{{hobby.display}}
完整示例
模板
rush:xhtml;">