使用 Angular 中的反应式表单根据条件动态添加行 [英] Dynamically Add Rows Based on Condition Using Reactive Forms in Angular

查看:15
本文介绍了使用 Angular 中的反应式表单根据条件动态添加行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是否可以在 Angular 反应形式中根据特定条件添加行?如果是,请帮我做.我已经在我的 stackblitz 中开始了它.请检查此链接https://stackblitz.com/edit/dynamically-add-rows

Is it possible that i will be able to add rows based on certain condition in Angular reactive forms? If yes, please help me do it. I've already started it in my stackblitz. Please check this link https://stackblitz.com/edit/dynamically-add-rows

this.addForm.get("items").valueChanges.subscribe(val => {
      console.log(val);
      if (val === true) {
        this.addForm.get("items_value").setValue("yes");

        // How to add row here?
        this.addForm.addControl(rows, 'rows')
      }
      if (val === false) {
        this.addForm.get("items_value").setValue("no");

        // How to remove row here?
        this.addForm.removeControl(rows, 'rows')
      }
    });

推荐答案

这里是编辑好的 stackblitz :https://stackblitz.com/edit/dynamically-add-rows-gk4veg?file=app%2Fapp.component.html

here is the edited stackblitz :https://stackblitz.com/edit/dynamically-add-rows-gk4veg?file=app%2Fapp.component.html

这篇关于使用 Angular 中的反应式表单根据条件动态添加行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆