加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angular2 / 4.当FormsModule内置时,我应该使用Reactive Forms?

发布时间:2020-12-17 06:49:22 所属栏目:安全 来源:网络整理
导读:任何人都可以解释原因 我需要用它们吗? 在哪些情况下,与FormsModule相比,Reactive Forms使我的生活更简单,更好? 解决方法 基本上,如果您有非常简单的要求,FormsModule就足够了.我的第一个表单都是FormsModule.如果您只需要将表单输入绑定到模型属性,它们就
任何人都可以解释原因
我需要用它们吗?

在哪些情况下,与FormsModule相比,Reactive Forms使我的生活更简单,更好?

解决方法

基本上,如果您有非常简单的要求,FormsModule就足够了.我的第一个表单都是FormsModule.如果您只需要将表单输入绑定到模型属性,它们就很棒.

ReactiveFormsModule是第一次使用时设置的更大痛苦,但它们可以为您提供更多控制.因为您在组件中创建表单元素,所以您可以轻松地执行以下操作:

>在任何点读取/写入输入的值(甚至在构建视图之前)
>定义高级验证规则,包括异步验证器(例如:在用户填写注册表的其余部分时,检查服务器是否可以输入用户名)
>当值发生变化时,立即通知并立即做出反应
>访问本机HTML表单元素
>重置表格……
>如果您关心单元测试(良好的编码员应该),反应形式更容易测试,因为它们可以被强制操纵.使用FormsModule,只是在单元测试中获取对表单实例的引用是一件痛苦的事.

自从我进行切换后,我没有回到FormsModule. See the docs

Angular reactive forms facilitate a reactive style of programming that favors explicit management of the data flowing between a non-UI data model (typically retrieved from a server) and a UI-oriented form model that retains the states and values of the HTML controls on screen. Reactive forms offer the ease of using reactive patterns,testing,and validation.

With reactive forms,you create a tree of Angular form control objects in the component class and bind them to native form control elements in the component template,using techniques described in this guide.

You create and manipulate form control objects directly in the component class. As the component class has immediate access to both the data model and the form control structure,you can push data model values into the form controls and pull user-changed values back out. The component can observe changes in form control state and react to those changes.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读