简体   繁体   English

模板驱动形式和反应形式之间的实际区别是什么?

[英]What are the practical differences between template-driven and reactive forms?

I have been reading about Angular2 new Forms API and it seems that there are two approaches on forms, one is Template driven forms other is reactive or model-driven forms.我一直在阅读有关 Angular2 新表单 API 的信息,似乎表单有两种方法,一种是模板驱动的表单,另一种是反应式或模型驱动的表单。

I would like to know the practical difference between the two, not the difference in syntax (obviously) but practical uses and which approach benefits more in different scenarios.我想知道两者之间的实际区别,不是语法上的差异(显然)而是实际用途以及哪种方法在不同场景中受益更多。 Also, is there a performance gain in choosing one over the other?此外,选择一个而不是另一个是否有性能提升? And if yes, why?如果是,为什么?

Template Driven Forms Features模板驱动的表单功能

  • Easy to use易于使用
  • Suitable for simple scenarios and fails for complex scenarios适合简单场景,不适合复杂场景
  • Similar to AngularJS类似于 AngularJS
  • Two way data binding(using [(NgModel)] syntax)双向数据绑定(使用[(NgModel)]语法)
  • Minimal component code最少的组件代码
  • Automatic track of the form and its data(handled by Angular)自动跟踪表单及其数据(由 Angular 处理)
  • Unit testing is another challenge单元测试是另一个挑战

Reactive Forms Features反应式功能

  • More flexible, but needs a lot of practice更灵活,但需要大量练习
  • Handles any complex scenarios处理任何复杂的场景
  • No data binding is done (immutable data model preferred by most developers)不进行数据绑定(大多数开发人员首选的不可变数据模型)
  • More component code and less HTML markup更多的组件代码和更少的 HTML 标记
  • Reactive transformations can be made possible such as可以使反应性转换成为可能,例如
    • Handling a event based on a debounce time根据去抖动时间处理事件
    • Handling events when the components are distinct until changed当组件不同时处理事件直到更改
    • Adding elements dynamically动态添加元素
  • Easier unit testing更简单的单元测试

I think that it´sa discussion about code , strategy and user experience .我认为这是关于代码策略用户体验的讨论。

In summary we change for template-driven approach which is more easy to work with it, to reactive (in model-driven approach) for giving us more control , that results in a better testable form by leveraging a decoupling between the HTML (design/CSS team can work here) and component's business rules (angular/js specialist members) and to improve the user experience with features like reactive transformations, correlated validations and handle complex scenarios as runtime validation rules and dynamic fields duplication.总之,我们将更易于使用的模板驱动方法更改为反应式(在模型驱动方法中)为我们提供更多控制,从而通过利用 HTML(设计/ CSS 团队可以在这里工作)和组件的业务规则(angular/js 专家成员),并通过响应式转换、相关验证等功能改善用户体验,并处理复杂的场景,如运行时验证规则和动态字段重复。

This article is a good reference about it: Angular 2 Forms - Template Driven and Model Driven Approaches这篇文章是一个很好的参考: Angular 2 Forms - Template Driven and Model Driven Approaches

Here is the summary of comparision between template driven and reactive forms explained by DeborahK (Deborah Kurata) well,这里是由 DeborahK (Deborah Kurata) 很好地解释的模板驱动和反应形式之间的比较总结, 在此处输入图片说明

Reactive forms:反应形式:

  • reusable,可重复使用,
  • more robust,更健壮,
  • testable,可测试,
  • more scalable更具可扩展性

Template-driven forms:模板驱动的表单:

  • easy to add,容易添加,
  • less scalable,可扩展性较差,
  • basic form requirements基本形式要求

In summaries , if forms are very important for your app, or reactive pattern are used in your app, you should use reactive forms .Otherwise your app have basic and simple requirement for forms such as sign in, you should use template-driven forms .总结一下,如果表单对你的应用非常重要,或者你的应用中使用了响应式模式,你应该使用响应式表单。否则你的应用对登录等表单有基本和简单的要求,你应该使用模板驱动的表单

There is a angular offical link有角官方链接

Easiest way to know the difference between Reactive forms and Template-driven forms了解反应式表单和模板驱动表单之间区别的最简单方法

i can say if you want more control and scalability go with Reactive forms我可以说如果你想要更多的控制和可扩展性,请使用反应式表单

在此处输入图片说明

Template Driven Forms :模板驱动的表单:

imported using FormsModule使用 FormsModule 导入

Forms built with ngModel directive can only be tested in an end to end test because this requires the presence of a DOM使用 ngModel 指令构建的表单只能在端到端测试中进行测试,因为这需要存在 DOM

The form value would be available in two different places: the view model ie ngModel表单值将在两个不同的地方可用:视图模型,即 ngModel

Form validation, as we add more and more validator tags to a field or when we start adding complex cross-field validations the readability of the form decreases表单验证,随着我们向字段添加越来越多的验证器标签,或者当我们开始添加复杂的跨字段验证时,表单的可读性会降低

Reactive Forms :反应形式:

Can generally used for large-scale applications一般可用于大规模应用

complex validation logic is actually simpler to implement复杂的验证逻辑实际上更容易实现

imported using ReactiveFormsModule使用 ReactiveFormsModule 导入

The form value would be available in two different places: the view model and the FormGroup表单值将在两个不同的地方可用:视图模型和 FormGroup

Easy to Unit Test : We can do that just by instantiating the class, setting some values in the form controls and perform assertions against the form global valid state and the validity state of each control.易于单元测试:我们可以通过实例化类,在表单控件中设置一些值并针对表单全局有效状态和每个控件的有效性状态执行断言来做到这一点。

Use of Observables for reactive programming使用 Observables 进行反应式编程

For example: a password field and a password confirmation field need to be identical例如:密码字段和密码确认字段需要相同

Reactive way : we just need to write a function and plug it into the FormControl反应式:我们只需要编写一个函数并将其插入FormControl

Template-Driven Way : we need to define a directive and somehow pass it the value of the two fields模板驱动方式:我们需要定义一个指令并以某种方式将两个字段的值传递给它

https://blog.angular-university.io/introduction-to-angular-2-forms-template-driven-vs-model-driven/ https://blog.angular-university.io/introduction-to-angular-2-forms-template-driven-vs-model-driven/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM