-
Notifications
You must be signed in to change notification settings - Fork 6.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
fix(input): properly determine input value #2455
Conversation
@devversion I think this might break turning off floating placeholders, e.g.: <md-input-container [floatingPlaceholder]="false">
...
</md-input-container> Now that we're not listening to input events, the placeholder sticks around as the user types. |
Great catch. I've chatted with @kara about this and we thought about alternate approaches than listening to the
Similar to angular/angular#default_value_accessor.ts, which should also trigger a change detection. Need confirmation that the |
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.
Nitpick on the comment, otherwise looks good. @mmalerba will probably want to take a look too.
_onInput() { this.value = this._elementRef.nativeElement.value; } | ||
_onInput() { | ||
// This is a noop function and is used to let Angular recognize the changes | ||
// to the input element while typing. Listening to the `input` event is similar to NgControls. |
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.
Nitpick: Can you add some more detail here? I'd also add that if you're using one of the FormsModule or ReactiveFormsModule directives, the input event is already watched and will kick off change detection appropriately. But that if you're not using one of these and you've disabled the floating placeholder, we need to ensure that change detection still runs immediately on input to hide the placeholder.
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.
Updated the comment.
LGTM, waiting on @mmalerba |
@devversion Sorry, one more rebase |
Right now the `MdInputDirective` tries to cache the `value` of the input. To do this, the `MdInputDirective` needs to listen for `NgControl` value changes and for native `(change)` events. This will be problematic when a value is set directly to the input element (using `[value]` property binding) because Angular is only able to recognize this change in the first ChangeDetection. ```html <md-input-container> <input md-input [value]="myValue" placeholder="Label"> </md-input-container> ``` The approach of updating the value in the `ngAfterViewInit` lifecycle hook, will result in a binding change while being in a ChangeDetection, which leads to a ChangeDetection error. ``` Expression has changed after it was checked. Previous value: 'true'. Current value: 'false'. ``` Fixes angular#2441. Fixes angular#2363
89c4310
to
778f709
Compare
@kara Done. |
hey @devversion I still get this issue if there is a condition check in |
@abdulhaq-e I'm still having the same issue as you using master, if I don't use it in ngAfterViewInit it seems to crash the form and not display anything. |
@RicardoVaranda See this comment: #2837 (comment) You need to use the |
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. |
Right now the
MdInputDirective
tries to cache thevalue
of the input.To do this, the
MdInputDirective
needs to listen forNgControl
value changes and for native(change)
events.This will be problematic when a value is set directly to the input element (using
[value]
property binding) because Angular is only able to recognize this change in the first ChangeDetection.The approach of updating the value in the
ngAfterViewInit
lifecycle hook, will result in a binding change while being in a ChangeDetection, which leads to a ChangeDetection error.The credits for the
should update the value when using FormControl.setValue
test go to @crisbetoFixes #2441. Fixes #2363. Fixes #2477