angular-docs-cn/tools
Kara Erickson 8fb34bcd43 refactor(forms): deprecate ngModel usage on same field as formControl (#22633)
Support for using the `ngModel` input property and `ngModelChange`
event with reactive form directives has been deprecated in
Angular v6 and will be removed in Angular v7.

Now deprecated:
```html
<input [formControl]="control" [(ngModel)]="value">
```

```ts
this.value = 'some value';
```

This has been deprecated for a few reasons. First, developers have
found this pattern confusing. It seems like the actual `ngModel`
directive is being used, but in fact it's an input/output property
named `ngModel` on the reactive form directive that simply approximates
(some of) its behavior. Specifically, it allows getting/setting the
value and intercepting value events. However, some of `ngModel`'s other
features - like delaying updates with`ngModelOptions` or exporting the
directive - simply don't work, which has understandably caused some
confusion.

In addition, this pattern mixes template-driven and reactive forms
strategies, which we generally don't recommend because it doesn't take
advantage of the full benefits of either strategy. Setting the value in
the template violates the template-agnostic principles behind reactive
forms, whereas adding a FormControl/FormGroup layer in the class removes
the convenience of defining forms in the template.

To update your code before v7, you'll want to decide whether to stick
with reactive form directives (and get/set values using reactive forms
patterns) or switch over to template-driven directives.

After (choice 1 - use reactive forms):

```html
<input [formControl]="control">
```

```ts
this.control.setValue('some value');
```

After (choice 2 - use template-driven forms):

```html
<input [(ngModel)]="value">
```

```ts
this.value = 'some value';
```

You can also choose to silence this warning by providing a config for
`ReactiveFormsModule` at import time:

```ts
imports: [
  ReactiveFormsModule.withConfig({warnOnNgModelWithFormControl: 'never'});
]
```

Alternatively, you can choose to surface a separate warning for each
instance of this pattern with a config value of `"always"`. This may
help to track down where in the code the pattern is being used as the
code is being updated.

Note: `warnOnNgModelWithFormControl` is set up as deprecated so that it
can be removed in v7 when it is no longer needed. This will not display
properly in API docs yet because dgeni doesn't yet support deprecating
properties in object literals, but we have an open issue to resolve the
discrepancy here: https://github.com/angular/angular/issues/22640.

PR Close #22633
2018-03-07 20:47:53 -08:00
..
build build: update npm dependencies (#19328) 2017-09-22 13:20:52 -07:00
cjs-jasmine build(ivy): create hello world rollup (#22004) 2018-02-06 08:25:22 -08:00
code.angularjs.org build: use subshells when changing directories 2017-05-12 12:19:51 -07:00
gulp-tasks test: include router in public_api_guard tests (#22628) 2018-03-07 10:56:27 -08:00
karma build: update npm dependencies (#19328) 2017-09-22 13:20:52 -07:00
npm build: remove local yarn (#19981) 2017-11-02 15:11:34 -07:00
public_api_guard refactor(forms): deprecate ngModel usage on same field as formControl (#22633) 2018-03-07 20:47:53 -08:00
source-map-test build: update npm dependencies (#19328) 2017-09-22 13:20:52 -07:00
symbol-extractor test: Add bundle symbol extractor tool (#22002) 2018-02-13 11:28:54 -08:00
testing build: move repeated tsconfig attributes to a macro (#20964) 2018-01-10 12:30:19 -08:00
travis build: update npm dependencies (#19328) 2017-09-22 13:20:52 -07:00
tree-shaking-test refactor: update paths from modules/@angular to packages 2017-03-08 16:29:28 -08:00
ts-api-guardian test(bazel): Build and test ts-api-guardian locally (#22544) 2018-03-02 15:00:00 -08:00
tsc-watch build: remove references to `tsc-wrapped` (#19298) 2017-09-21 13:55:52 -07:00
tslint build: update rollup lint rule from bad merge (#20047) 2017-10-30 23:43:49 -04:00
validate-commit-message build: make git revert messages valid (#22339) 2018-02-21 11:05:35 -08:00
yarn build: upgrade yarn to 1.3.2 (#21406) 2018-01-10 12:32:26 -08:00
BUILD.bazel test(bazel): Build and test ts-api-guardian locally (#22544) 2018-03-02 15:00:00 -08:00
bazel.rc build: disable bazel-out symlink (#22375) 2018-02-22 13:31:59 -08:00
bazel_stamp_vars.sh feat(bazel): introduce a binary stamping feature (#22176) 2018-02-15 14:08:53 -08:00
check-environment.js build: update npm dependencies (#19328) 2017-09-22 13:20:52 -07:00
defaults.bzl feat(bazel): add an ng_package rule (#22221) 2018-02-23 11:19:04 -08:00
jsconfig.json chore(vscode): add jsconfig.json and remove .settings dir 2015-10-29 13:53:21 +00:00
tsconfig.json test(bazel): Build and test ts-api-guardian locally (#22544) 2018-03-02 15:00:00 -08:00
types.d.ts fix(fakeAsync): have fakeAsync use Proxy zone. (#10797) 2016-08-19 12:10:53 -07:00
utils.inc feat(bundle): add script to push bundles to code.angularjs.org 2015-04-14 14:50:53 -07:00