Angular 2/4:反应式表单是同步的,而模板驱动的表单是异步的,如何? [英] Angular 2/4 : Reactive forms are synchronous while template-driven forms are asynchronous, How?

查看:70
本文介绍了Angular 2/4:反应式表单是同步的,而模板驱动的表单是异步的,如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读angular.io的文档(反应性表单是同步的),尝试理解反应式(它们是同步的,而模板驱动的形式是异步的).

I was going through the doc of angular.io (Reactive forms are synchronous), trying to understand the reactive forms (how they are synchronous and template driven forms are asynchronous).

但是该文档对example的解释不足.谁能帮助我了解模板驱动的表单是异步的,而反应式表单是同步的吗?

But the doc doesn't have enough explanation with example . Can anyone help me understand how template driven forms are asynchronous and reactive forms are synchronous?

我试图在互联网上浏览很多博客,但没有得到答案.

I tried to explore a lot of blogs on the internet but didn't get an answer.

感谢您的帮助.

推荐答案

在同一文档中仔细阅读以下内容:

Read this line careful in same documentation :

反应性表单是同步的(当您通过代码创建控件时) 在反应式表单中,您可以用代码创建整个表单控件树.由于所有控件始终可用,因此您可以立即更新值或在父窗体的后代中进行追溯.

Reactive forms are synchronous (as you create controls from you code) In reactive forms, you create the entire form control tree in code. You can immediately update a value or drill down through the descendants of the parent form because all controls are always available.

模板驱动的表单是异步的(因为它委派了创建控件的任务) 模板驱动的表单将其表单控件的创建委托给指令.为了避免检查后更改"错误,这些指令花费了一个以上的周期来构建整个控制树.这意味着在操作组件类中的任何控件之前,您必须先打勾.

template-driven forms are asynchronous (as it delegate task of creation of control) Template-driven forms delegate creation of their form controls to directives. To avoid "changed after checked" errors, these directives take more than one cycle to build the entire control tree. That means you must wait a tick before manipulating any of the controls from within the component class.

在您驱动的模板中,编写 [NgModel]或[NgForm](伪指令),这些任务将以html形式在网页上创建控件的任务,这就是它变为异步的方式.

In template driven from you write [NgModel] or [NgForm] (directives) that will take task of creation of your control on web page in html, that is how it becomes asynchronous.

如果您连接了钩子 ngAfterViewInit生命周期钩子,您将很容易发现它们之间的区别,

If you attached hook ngAfterViewInit lifecycle hook you will find difference between them easily ,

在反应式表单中,您可以找到不受模板驱动的位置的控制权.

In Reactive forms you find control where in Template driven from you dont.

这篇关于Angular 2/4:反应式表单是同步的,而模板驱动的表单是异步的,如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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