Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(forms): deprecate ngModel usage on same field as formControl #22633

Closed
wants to merge 1 commit into from

Conversation

kara
Copy link
Contributor

@kara kara commented Mar 7, 2018

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:

<input [formControl]="control" [(ngModel)]="value">
this.value = 'some value'; 

This is being 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 withngModelOptions 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):

<input [formControl]="control">
this.control.setValue('some value');

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

<input [(ngModel)]="value">
this.value = 'some value'; 

This PR adds a deprecation warning that will show once in dev mode when using the pattern above.
You can choose to silence this warning by providing a config for ReactiveFormsModule at import time:

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.

@mary-poppins
Copy link

You can preview 7bf5eb5 at https://pr22633-7bf5eb5.ngbuilds.io/.

@kara kara added action: review The PR is still awaiting reviews from at least one requested reviewer refactoring Issue that involves refactoring or code-cleanup and removed state: WIP labels Mar 7, 2018
@mary-poppins
Copy link

You can preview 37e266c at https://pr22633-37e266c.ngbuilds.io/.

@@ -35,4 +36,10 @@ export class FormsModule {
exports: [InternalFormsSharedModule, REACTIVE_DRIVEN_DIRECTIVES]
})
export class ReactiveFormsModule {
static withConfig(opts: {silenceNgModelWarning: boolean}): ModuleWithProviders {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we deprecate the silenceNgModelWarning option immediately? It will allow for removal with Angular 7 once the property does not make sense anymore.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed with @IgorMinar and we think this is probably a good idea.

@@ -491,6 +491,9 @@ export declare class RadioControlValueAccessor implements ControlValueAccessor,

/** @stable */
export declare class ReactiveFormsModule {
static withConfig(opts: {
silenceNgModelWarning: boolean;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest that you change the option to something very explicit. e.g. warnOnNgModelWithFormControl and make it a string with options: "never" | "once" | "always"

@IgorMinar
Copy link
Contributor

btw awesome commit message. thanks for documenting this change well.

@kara kara force-pushed the deprecate-ngModel branch 2 times, most recently from fd25c6f to 89285b9 Compare March 8, 2018 01:20
@mary-poppins
Copy link

You can preview fd25c6f at https://pr22633-fd25c6f.ngbuilds.io/.

Copy link
Contributor

@IgorMinar IgorMinar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

otherwise lgtm

@@ -35,4 +36,15 @@ export class FormsModule {
exports: [InternalFormsSharedModule, REACTIVE_DRIVEN_DIRECTIVES]
})
export class ReactiveFormsModule {
static withConfig(opts: {
/** @deprecated from v6 */ warnOnNgModelWithFormControl: 'never' | 'once' | 'always'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from => as of

@@ -74,4 +74,31 @@ export class ReactiveErrors {
});
`);
}

static ngModelWarning(directiveName: string): void {
console.warn(`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will we be able to tree-shake/DCE this? I think only closure will be able to do it. am I wrong?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest you make the error message shorter and just add a link to our docs where we explain this in greater detail.

@mary-poppins
Copy link

You can preview 89285b9 at https://pr22633-89285b9.ngbuilds.io/.

@kara kara force-pushed the deprecate-ngModel branch 2 times, most recently from 7a04ccf to 3704a23 Compare March 8, 2018 02:06
@mary-poppins
Copy link

You can preview 7a04ccf at https://pr22633-7a04ccf.ngbuilds.io/.

@kara
Copy link
Contributor Author

kara commented Mar 8, 2018

presubmit

@mary-poppins
Copy link

You can preview 3704a23 at https://pr22633-3704a23.ngbuilds.io/.

@mary-poppins
Copy link

You can preview edf15f3 at https://pr22633-edf15f3.ngbuilds.io/.

@kara kara added target: major This PR is targeted for the next major release and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Mar 8, 2018
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: angular#22640.
@mary-poppins
Copy link

You can preview 7a309cc at https://pr22633-7a309cc.ngbuilds.io/.

@kara kara added the action: merge The PR is ready for merge by the caretaker label Mar 8, 2018
@kara kara closed this in 8fb34bc Mar 8, 2018
leo6104 pushed a commit to leo6104/angular that referenced this pull request Mar 25, 2018
…angular#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: angular#22640.

PR Close angular#22633
@kara kara deleted the deprecate-ngModel branch October 13, 2018 01:09
kara added a commit to kara/angular that referenced this pull request Feb 27, 2019
…rectives

BREAKING CHANGE:

In v6, we deprecated support for the ngModel and ngModelChange inputs on
reactive form directives. We are now removing these deprecated APIs for
v8.  See the original change here for more info on why:
angular#22633.
kara added a commit to kara/angular that referenced this pull request Feb 27, 2019
…rectives

BREAKING CHANGE:

In v6, we deprecated support for the ngModel and ngModelChange inputs on
reactive form directives. We are now removing these deprecated APIs for
v8.  See the original change here for more info on why:
angular#22633.
kara added a commit to kara/angular that referenced this pull request Feb 27, 2019
…rectives

Closes angular#12708.

BREAKING CHANGE:

In v6, we deprecated support for the ngModel and ngModelChange inputs on
reactive form directives. We are now removing these deprecated APIs for
v8.  See the original change here for more info on why:
angular#22633.
kara added a commit to kara/angular that referenced this pull request Feb 27, 2019
…rectives

Closes angular#12708.

BREAKING CHANGE:

In v6, we deprecated support for the ngModel and ngModelChange inputs on
reactive form directives. We are now removing these deprecated APIs for
v8.  See the original change here for more info on why:
angular#22633.
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 14, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker area: forms cla: yes refactoring Issue that involves refactoring or code-cleanup target: major This PR is targeted for the next major release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants