惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
Google DeepMind News
Google DeepMind News
G
Google Developers Blog
Martin Fowler
Martin Fowler
MongoDB | Blog
MongoDB | Blog
月光博客
月光博客
Jina AI
Jina AI
宝玉的分享
宝玉的分享
人人都是产品经理
人人都是产品经理
D
DataBreaches.Net
V
V2EX
WordPress大学
WordPress大学
T
The Blog of Author Tim Ferriss
Last Week in AI
Last Week in AI
B
Blog
博客园 - 叶小钗
小众软件
小众软件
Stack Overflow Blog
Stack Overflow Blog
P
Proofpoint News Feed
A
About on SuperTechFans
J
Java Code Geeks
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Y
Y Combinator Blog
Microsoft Security Blog
Microsoft Security Blog

zodream梦想开源/个人编程日记

文件解析笔记-zodream梦想开源/个人编程日记 密码本开发笔记之读写与保存-zodream梦想开源/个人编程日记 SkiaSharp 把 pixel byte[] 转成 SKBitmap-zodream梦想开源/个人编程日记 nas 使用 Docker 安装 gogs-zodream梦想开源/个人编程日记 复制 android 手机中的文件到电脑-zodream梦想开源/个人编程日记 周报:寻找优质的周刊-zodream梦想开源/个人编程日记 开发日志:对Markdown的代码块新增引用来源支持-zodream梦想开源/个人编程日记 周报:怎么写技术类的教程文章-zodream梦想开源/个人编程日记 css display:flex 布局尺寸超出问题-zodream梦想开源/个人编程日记 周报:SEO优化的思考-zodream梦想开源/个人编程日记 Edge 浏览器不适用 Edge Image Viewer 打开图片 -zodream梦想开源/个人编程日记 SEO 学习笔记(一) 内容来源-zodream梦想开源/个人编程日记 PHP 实现双因素身份认证(2FA)-zodream梦想开源/个人编程日记 WPF MVVM 获取List 多选数据-zodream梦想开源/个人编程日记 Burp Suite 抓包-zodream梦想开源/个人编程日记 使用 indexnow 注意事项-zodream梦想开源/个人编程日记 Godot 使用字体图标 例如: Iconfont、FontAwesome-zodream梦想开源/个人编程日记 angular 15 对指定页面进行访问限制-zodream梦想开源/个人编程日记 CSS 使用 column-count 实现瀑布流出现内容分割的解决办法-zodream梦想开源/个人编程日记 input 确认按键事件在手机端不生效-zodream梦想开源/个人编程日记 C# 使用socket 进行通讯-zodream梦想开源/个人编程日记 Maui开发中Windows应用开启管理员权限-zodream梦想开源/个人编程日记 Maui 中自定义控件-zodream梦想开源/个人编程日记 angular 14 使用 ng-template 实现tree 结构显示-zodream梦想开源/个人编程日记 c# 动态安装和卸载dll-zodream梦想开源/个人编程日记 慎用 CompositionTarget.Rendering-zodream梦想开源/个人编程日记 c# 重写 c++ 程序笔记:数据初始化-zodream梦想开源/个人编程日记 源码编译 aseprite-zodream梦想开源/个人编程日记 记录一下字符串分隔split各语言之间的不同-zodream梦想开源/个人编程日记 c# Gzip解码无头内容-zodream梦想开源/个人编程日记
angular 11 FormBuilder 中 FormGroup 和 FormArray 使用-zod...
zodream · 2021-02-17 · via zodream梦想开源/个人编程日记

angular 11 FormBuilder 中 FormGroup 和 FormArray 使用

导入模块

@NgModule({
    imports: [
        ReactiveFormsModule,
    ],
})
export class AppModule {}

123456

注入组件

export class EditComponent {
    constructor(
        private fb: FormBuilder,
    ) { }
}

12345

普通表单具体代码

初始化表单

export class EditComponent {
    public form = this.fb.group({
        name: ['', Validators.required],
    });

    constructor(
        private fb: FormBuilder,
    ) { }
}

123456789

绑定表单页面

<form [formGroup]="form" (ngSubmit)="tapSubmit()">
    <div class="form-group row">
        <label for="name" class="col-md-3 col-form-label">称呼</label>
        <div class="col-md-9">
            <input type="text" class="form-control" formControlName="name" id="name" placeholder="请输入称呼">
        </div>
    </div>
    <div class="row">
        <button class="btn btn-primary offset-md-3">保存</button>
    </div>
</form>

1234567891011

设置值

this.form.patchValue({
    name: res.name,
});

123

特殊表单项:一个可变的数组

FormArray 必须为 FormGroup 的子项

初始化表单

export class EditComponent {
    public form = this.fb.group({
        name: ['', Validators.required],
        children: this.fb.array([])
    });

    constructor(
        private fb: FormBuilder,
    ) { }

    get children() {
        return this.form.get('children') as FormArray;
    }

    public addChild() {
        this.children.push(this.fb.group({
            name: ['', Validators.required],
            label: ['', Validators.required],
            required: [false],
            only: [false],
        }));
    }

    public removeChild(i: number) {
        this.children.removeAt(i);
    }
}

123456789101112131415161718192021222324252627

绑定表单页面

<form [formGroup]="form" (ngSubmit)="tapSubmit()">
    <div class="form-group row">
        <label for="name" class="col-md-3 col-form-label">称呼</label>
        <div class="col-md-9">
            <input type="text" class="form-control" formControlName="name" id="name" placeholder="请输入称呼">
        </div>
    </div>
    <table class="table table-hover">
        <thead>
            <tr>
                <th>名称</th>
                <th>别名</th>
                <th>是否必填</th>
                <th>不公开</th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            <ng-container *ngFor="let item of children.controls; let i = index">
                <tr [formGroup]="item">
                    <td>
                        <input type="text" formControlName="name" class="form-control">
                    </td>
                    <td>
                        <input type="text" formControlName="label" class="form-control">
                    </td>
                    <td>
                        <input type="checkbox" formControlName="required" value="1"  >
                    </td>
                    <td>
                        <input type="checkbox" formControlName="only" value="1" >
                    </td>
                    <td>
                        <i class="iconfont icon-close" (click)="removeChild(i)"></i>
                    </td>
                </tr>
            </ng-container>
        </tbody>
        <tfoot>
            <tr>
                <td colspan="5">
                    <i class="iconfont icon-plus" (click)="addChild()"></i>
                </td>
            </tr>
        </tfoot>
    </table>
    <div class="row">
        <button class="btn btn-primary offset-md-3">保存</button>
    </div>
</form>

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950

设置值

for (const item of items) {
    this.children.push(this.fb.group(item));
}

123

转载请保留原文链接: https://zodream.cn/blog/id/193.html