-
Notifications
You must be signed in to change notification settings - Fork 24.7k
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
Conversation
You can preview 7bf5eb5 at https://pr22633-7bf5eb5.ngbuilds.io/. |
You can preview 37e266c at https://pr22633-37e266c.ngbuilds.io/. |
packages/forms/src/form_providers.ts
Outdated
@@ -35,4 +36,10 @@ export class FormsModule { | |||
exports: [InternalFormsSharedModule, REACTIVE_DRIVEN_DIRECTIVES] | |||
}) | |||
export class ReactiveFormsModule { | |||
static withConfig(opts: {silenceNgModelWarning: boolean}): ModuleWithProviders { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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"
btw awesome commit message. thanks for documenting this change well. |
fd25c6f
to
89285b9
Compare
You can preview fd25c6f at https://pr22633-fd25c6f.ngbuilds.io/. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
otherwise lgtm
packages/forms/src/form_providers.ts
Outdated
@@ -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' |
There was a problem hiding this comment.
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(` |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
You can preview 89285b9 at https://pr22633-89285b9.ngbuilds.io/. |
7a04ccf
to
3704a23
Compare
You can preview 7a04ccf at https://pr22633-7a04ccf.ngbuilds.io/. |
You can preview 3704a23 at https://pr22633-3704a23.ngbuilds.io/. |
You can preview edf15f3 at https://pr22633-edf15f3.ngbuilds.io/. |
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.
You can preview 7a309cc at https://pr22633-7a309cc.ngbuilds.io/. |
…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
…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.
…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.
…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.
…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.
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Support for using the
ngModel
input property andngModelChange
event with reactive form directives has been deprecated in Angular v6 and will be removed in Angular v7.Now deprecated:
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 namedngModel
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 ofngModel
'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):
After (choice 2 - use template-driven forms):
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: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.