Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
465 views
in Technique[技术] by (71.8m points)

angular - 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.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...