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

查看:21
本文介绍了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天全站免登陆