components: Textarea in MatForMield can cause ExpressionChangedAfterItHasBeenCheckedError
Bug, feature request, or proposal:
BUG “@angular/material”: “5.2.5”,
What is the expected behavior?
No exception
What is the current behavior?
Exception
This is the first time I have notice that and I cannot reproduce this on plain project. I don’t know what causes it and how to reproduce for unit testing.
Having following markup:
<mat-form-field class="summary no-ink-bar">
<textarea matInput placeholder="Summary:" readonly>{{request.summary}}</textarea>
</mat-form-field>
Causes following error
Angular is running in the development mode. Call enableProdMode() to enable the production mode. core.js:3688 ERROR Error: "ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'mat-form-field-should-float: false'. Current value: 'mat-form-field-should-float: true'." node_modulesangular/core/esm5/core.js:10033:32node_modulesangular/core/esm5/core.js:10011:12node_modulesangular/core/esm5/core.js:10178:15node_modulesangular/core/esm5/core.js:14254:9node_modulesangular/core/esm5/core.js:14203:9node_modulesangular/core/esm5
I am fixing this by setting floatLabel=“always” so
<mat-form-field class="summary no-ink-bar" floatLabel="always"> <textarea matInput placeholder="Summary:" readonly>{{request.summary}}</textarea> </mat-form-field>
removes that exception.
Maybe someone will have similar issue and will know how to reproduce
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 49
- Comments: 20 (5 by maintainers)
Commits related to this issue
- Fixes console error with address forms Know bug https://github.com/angular/material2/issues/12070 — committed to MIKEL-Inc/resume by chrisBosse 5 years ago
- fix(input): label floating state not updated when using text binding inside textarea Fixes the state that tells the form field whether the label should float not being up-to-date if the consumer is u... — committed to crisbeto/material2 by crisbeto 5 years ago
- fix(input): label floating state not updated when using text binding inside textarea Fixes the state that tells the form field whether the label should float not being up-to-date if the consumer is u... — committed to crisbeto/material2 by crisbeto 5 years ago
- fix(input): label floating state not updated when using text binding inside textarea Fixes the state that tells the form field whether the label should float not being up-to-date if the consumer is u... — committed to crisbeto/material2 by crisbeto 5 years ago
- fix(input): label floating state not updated when using text binding inside textarea Fixes the state that tells the form field whether the label should float not being up-to-date if the consumer is u... — committed to crisbeto/material2 by crisbeto 5 years ago
- fix(input): label floating state not updated when using text binding inside textarea Fixes the state that tells the form field whether the label should float not being up-to-date if the consumer is u... — committed to crisbeto/material2 by crisbeto 5 years ago
- fix(input): label floating state not updated when using text binding inside textarea Fixes the state that tells the form field whether the label should float not being up-to-date if the consumer is u... — committed to crisbeto/material2 by crisbeto 5 years ago
- fix(input): label floating state not updated when using text binding inside textarea Fixes the state that tells the form field whether the label should float not being up-to-date if the consumer is u... — committed to crisbeto/material2 by crisbeto 5 years ago
- fix(input): label floating state not updated when using text binding inside textarea Fixes the state that tells the form field whether the label should float not being up-to-date if the consumer is u... — committed to crisbeto/material2 by crisbeto 4 years ago
- fix(material/input): label floating state not updated when using text binding inside textarea Fixes the state that tells the form field whether the label should float not being up-to-date if the cons... — committed to crisbeto/material2 by crisbeto 4 years ago
- feat: Toggle Canceled, Simulate Network Requests, Time Conflicts Add functionality to toggle view of canceled events. Add delay in mock API calls on reservation.service to simulate network requests. ... — committed to Batch-944-Adam-Ranieri/af-client by dcheun 3 years ago
Here’s a workaround until I can investigate, just use
value=""
instead of putting the text as content: https://stackblitz.com/edit/angular-material2-issue-y2tqhg?file=app/app.component.htmlI had a similar issue and in my understanding the issue is that:
mat-form-field
creates a form field with an empty value - themat-label
is displayed inside (thusmat-form-field-should-float: false
)mat-form-field
receives the new value and decides that now the label should float (mat-form-field-should-float: true
)In case with
value="..."
, (2) happens before (1), so there’s no problem. That’s also why it happens withtextarea
, but not withinput
.(For the record: I came here debugging a similar error in test - in my case the textarea received the value from ngModel. I ended up working around it by adding
floatLabel="always"
on themat-form-field
.)P.S. I had one more similar problem - this time it was:
ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'mat-focused: false'. Current value: 'mat-focused: true'.
. This was because I focused the field inngAfterViewInit
. When I moved the focusing tongAfterContentInit
, this problem was also gone.Hi, I’ve got both errors
mat-form-field-should-float
andmat-focused
forExpressionChangedAfterItHasBeenCheckedError
when using<input autofocus>
inside amat-form-field
with Angular Material v6.I can bypass the errors by using
floatLabel=never
and focusing the input inngAfterContentInit()
but it would be awesome to avoid using these approaches.Getting the same error here, too.
@fxck I’ve concluded that sometimes you just have to add delay(0) to the observable pipe to get it to update later than it wants to.
I’ve gone round and round in circles at various times on this - reread articles and SO posts but sometimes it just seems necessary.
I did create a pipeable operator that adds delay(0) so then I can put isLoggedIn$ | delay | async
I don’t have it to hand now but it’s trivial to write a pipeable operator as long as you remember to export and provide it properly.
<textarea matInput readonly>{{selectedList.summary}}</textarea>
error<textarea matInput readonly [(ngModel)]="selectedList.summary"></textarea>
works fineTry this
I’m getting the same error but only in unit tests.