From 4528d38124e0336558a20761ba754472eba20007 Mon Sep 17 00:00:00 2001 From: Zhicheng Wang Date: Thu, 27 Apr 2017 16:41:32 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BF=BB=E8=AF=91=E5=AE=8C=E4=BA=86=E5=93=8D?= =?UTF-8?q?=E5=BA=94=E5=BC=8F=E8=A1=A8=E5=8D=95=E4=B8=80=E7=AB=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../docs/ts/latest/guide/reactive-forms.jade | 530 +++++++++++++++++- public/docs/ts/latest/guide/webpack.jade | 2 +- 2 files changed, 500 insertions(+), 32 deletions(-) diff --git a/public/docs/ts/latest/guide/reactive-forms.jade b/public/docs/ts/latest/guide/reactive-forms.jade index 842cfa330d..9ff31b3f23 100644 --- a/public/docs/ts/latest/guide/reactive-forms.jade +++ b/public/docs/ts/latest/guide/reactive-forms.jade @@ -13,69 +13,69 @@ a#toc ## 目录 - *[Introduction to reactive forms](#intro) + * [Introduction to reactive forms](#intro) [响应式表单简介](#intro) - *[Setup](#setup) + * [Setup](#setup) [准备工作](#setup) - *[Create a data model](#data-model) + * [Create a data model](#data-model) [创建数据模型](#data-model) - *[Create a _reactive forms_ component](#create-component) + * [Create a _reactive forms_ component](#create-component) [创建*响应式表单*组件](#create-component) - *[Create its template file](#create-template) + * [Create its template file](#create-template) [创建模板文件](#create-template) - *[Import the _ReactiveFormsModule_](#import) + * [Import the _ReactiveFormsModule_](#import) [导入`ReactiveFormsModule`](#import) - *[Display the _HeroDetailComponent_](#update) + * [Display the _HeroDetailComponent_](#update) [显示`HeroDetailComponent`](#update) - *[Add a FormGroup](#formgroup) + * [Add a FormGroup](#formgroup) [添加FormGroup](#formgroup) - *[Taking a look at the form model](#json) + * [Taking a look at the form model](#json) [表单模型概览](#json) - *[Introduction to _FormBuilder_](#formbuilder) + * [Introduction to _FormBuilder_](#formbuilder) [`FormBuilder`简介](#formbuilder) - *[Validators.required](#validators) + * [Validators.required](#validators) - *[Nested FormGroups](#grouping) + * [Nested FormGroups](#grouping) [嵌套的FormGroup](#grouping) - *[Inspect _FormControl_ properties](#properties) + * [Inspect _FormControl_ properties](#properties) [`FormControl`的属性](#properties) - *[Set form model data using _setValue_ and _patchValue_](#set-data) + * [Set form model data using _setValue_ and _patchValue_](#set-data) [用`setValue`和`patchValue`设置表单的模型数据](#set-data) - *[Use _FormArray_ to present an array of _FormGroups_](#form-array) + * [Use _FormArray_ to present an array of _FormGroups_](#form-array) [用`FormArray`来表示`FormGroup`的数组](#form-array) - *[Observe control changes](#observe-control) + * [Observe control changes](#observe-control) [观察控件的变化](#observe-control) - *[Save form data](#save) + * [Save form data](#save) [保存表单数据](#save) @@ -692,32 +692,48 @@ a#formbuilder a#validators :marked - ### Validators.required + ### Validators.required - Though this guide doesn't go deeply into validations, here is one example that - demonstrates the simplicity of using `Validators.required` in reactive forms. + Though this guide doesn't go deeply into validations, here is one example that + demonstrates the simplicity of using `Validators.required` in reactive forms. + + 虽然本章不会深入讲解验证机制,但还是有一个例子来示范如何简单的在响应式表单中使用`Validators.required`。 - First, import the `Validators` symbol. + First, import the `Validators` symbol. + + 首先,导入`Validators`符号。 + +makeExample('reactive-forms/ts/src/app/hero-detail-3.component.ts', 'imports','src/app/hero-detail.component.ts (excerpt)')(format=".") :marked - To make the `name` `FormControl` required, replace the `name` - property in the `FormGroup` with an array. - The first item is the initial value for `name`; - the second is the required validator, `Validators.required`. + To make the `name` `FormControl` required, replace the `name` + property in the `FormGroup` with an array. + The first item is the initial value for `name`; + the second is the required validator, `Validators.required`. + + 要想让`name`这个`FormControl`是必须的,请把`FormGroup`中的`name`属性改为一个数组。第一个条目是`name`的初始值,第二个是`required`验证器:`Validators.required`。 +makeExample('reactive-forms/ts/src/app/hero-detail-3.component.ts', 'required','src/app/hero-detail.component.ts (excerpt)')(format=".") + .l-sub-section :marked Reactive validators are simple, composable functions. Configuring validation is harder in template-driven forms where you must wrap validators in a directive. + + 响应式验证器是一些简单、可组合的函数。 + 在模板驱动表单中配置验证器有些困难,因为我们必须把验证器包装进指令中。 + :marked Update the diagnostic message at the bottom of the template to display the form's validity status. + + 修改模板底部的诊断信息,以显示表单的有效性状态。 +makeExample('reactive-forms/ts/src/app/hero-detail-3.component.html', 'form-value-json','src/app/hero-detail.component.html (excerpt)')(format=".") :marked The browser displays the following: + + 浏览器会显示下列内容: figure.image-display img(src="/resources/images/devguide/reactive-forms/validators-json-output.png" width="400px" alt="Single FormControl") @@ -726,32 +742,54 @@ figure.image-display `Validators.required` is working. The status is `INVALID` because the input box has no value. Type into the input box to see the status change from `INVALID` to `VALID`. + `Validators.required`生效了,但状态还是`INVALID`,因为输入框中还没有值。 + 在输入框中输入,就会看到这个状态从`INVALID`变成了`VALID`。 + In a real app, you'd replace the diagnosic message with a user-friendly experience. + 在真实的应用中,我们要把这些诊断信息替换成用户友好的信息。 + :marked Using `Validators.required` is optional for the rest of the guide. It remains in each of the following examples with the same configuration. + + 对本章的其余部分,使用`Validators.required`是可有可无。 + 它保留在下面每一个使用了相同配置的范例中。 For more on validating Angular forms, see the [Form Validation](../cookbook/form-validation.html) guide. + + 要了解Angular表单验证器的更多知识,参见[Form验证器](../cookbook/form-validation.html)一章。 :marked ### More FormControls + + ### 更多的表单控件(FormControl) + A hero has more than a name. A hero has an address, a super power and sometimes a sidekick too. + 每个英雄可以有多个名字,还有一个住址、一项超能力,有时还会有一个副手。 + The address has a state property. The user will select a state with a ``框中选择一个州,我们会用`