模板驱动形式和反应形式之间的实际区别是什么? [英] What are the practical differences between template-driven and reactive forms?

查看:75
本文介绍了模板驱动形式和反应形式之间的实际区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读有关Angular2新Forms API的信息,似乎在表单上有两种方法,一种是由模板驱动的表单,另一种是反应性或模型驱动的表单.

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.

我想知道两者之间的实际区别,不是语法上的区别(很明显),而是实际用途,以及哪种方法在不同情况下的好处更大. 此外,选择一个而不是另一个是否会提高性能?如果是的话,为什么?

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?

推荐答案

模板驱动的表单功能

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

活动表单功能

  • 更灵活,但需要大量练习
  • 处理任何复杂的情况
  • 不进行数据绑定(大多数开发人员首选不可变数据模型)
  • 更多的组件代码和更少的HTML标记
  • 可以使反应性转换成为可能,例如
    • 根据去抖时间处理事件
    • 处理组件分开之前的事件,直到更改
    • 动态添加元素
    • 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
    • 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

      这篇关于模板驱动形式和反应形式之间的实际区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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