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

后端 未结 6 1601
遇见更好的自我
遇见更好的自我 2020-11-28 19:08

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.

6条回答
  •  庸人自扰
    2020-11-28 19:33

    Template Driven Forms Features

    • 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

    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
    • 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

提交回复
热议问题