“`javascript Angular: 当浏览器宽度变化时获取div元素的正确宽度值 “`

huangapple go评论98阅读模式
英文:

Angular: Get the correct width value of a div element when the browser width changes

问题

我有一个div元素,如果浏览器的宽度小于或等于dv元素的宽度,它会自动缩小。为此,我使用ngAfterViewChecked()。但是当div元素自动更改其宽度时,我会收到一个错误。

test.component.html:

  1. <div style="display:inline" #myContainer>
  2. <!-- 这只是一个示例 -->
  3. <div>
  4. <li>...</li>
  5. <li>...</li>
  6. <ng-container *ngIf="coOffset <= winWidth;">
  7. <li>...</li>
  8. <li>...</li>
  9. </ng-container>
  10. <div>
  11. </div>

test.component.ts:

  1. public coOffset: number;
  2. public winWidth: any;
  3. @ViewChild('myContainer') coRef: ElementRef;
  4. onResize(event) {
  5. this.winWidth = window.innerWidth;
  6. }
  7. ngAfterViewInit (): void {
  8. this.coOffset = this.coRef.nativeElement.offsetWidth;
  9. }
  10. ngAfterViewChecked(): void {
  11. this.coOffset = this.coRef.nativeElement.offsetWidth;
  12. this.cd.detectChanges();
  13. }
英文:

I have a div element that automatically shrinks if the width of the browser is less than or equal to the dv element width. For that I use ngAfterViewChecked(). But I get an error when the div element changes its width automatically.

test.component.html:

  1. &lt;div style=&quot;display:inline&quot; #myContainer&gt;
  2. //this is only an example
  3. &lt;div&gt;
  4. &lt;li&gt;...&lt;/li&gt;
  5. &lt;li&gt;...&lt;/li&gt;
  6. &lt;ng-container *ngIf=&quot;coOffset &lt;= winWidth;&quot;&gt;
  7. &lt;li&gt;...&lt;/li&gt;
  8. &lt;li&gt;...&lt;/li&gt;
  9. &lt;/ng-container&gt;
  10. &lt;div&gt;
  11. &lt;/div&gt;

test.component.ts:

  1. public coOffset: number;
  2. public winWidth: any;
  3. @ViewChild(&#39;myContainer&#39;) coRef: ElementRef;
  4. onResize(event) {
  5. this.winWidth = window.innerWidth;
  6. }
  7. ngAfterViewInit (): void {
  8. this.coOffset = this.coRef.nativeElement.offsetWidth;
  9. }
  10. ngAfterViewChecked(): void {
  11. this.coOffset = this.coRef.nativeElement.offsetWidth;
  12. this.cd.detectChanges();
  13. }

The error message reads:
ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: ...

I found many examples on the internet, but they couldn't solve my problem. Can someone help me please. Thanks

答案1

得分: 0

以下是代码部分的中文翻译:

唯一遗漏的是在ngAfterViewInit中缺少ChangeDetectorRef。我已经重现了相同的问题,并通过在ngAfterViewInit中添加detectChanges来修复它。
以下是ts文件的完整代码:

  1. import { ChangeDetectorRef, Component, HostListener, ViewChild } from '@angular/core';
  2. @Component({
  3. selector: 'app-root',
  4. templateUrl: './app.component.html',
  5. styleUrls: ['./app.component.sass']
  6. })
  7. export class AppComponent {
  8. public coOffset: any;
  9. public winWidth: any;
  10. @HostListener('window:resize', ['$event'])
  11. onResize() {
  12. this.resized();
  13. }
  14. @ViewChild('myContainer') coRef: any;
  15. constructor(public cd: ChangeDetectorRef)
  16. {
  17. this.winWidth = window.innerWidth;
  18. }
  19. ngAfterViewInit (): void {
  20. this.coOffset = this.coRef.nativeElement.offsetWidth;
  21. this.cd.detectChanges();
  22. }
  23. resized(): void {
  24. this.coOffset = this.coRef.nativeElement.offsetWidth;
  25. this.cd.detectChanges();
  26. }
  27. }

此外,请查看以下链接,以可视化地了解问题和解决方案:TestWise Replay | StackOverFlow QA - Angular: Get the correct width value of a div element when the browser width changes

英文:

The only thing you are missing is ChangeDetectorRef in ngAfterViewInit. I had reproduced the same problem and fixed by just adding detectChanges in ngAfterViewInit.
Here is complete code of ts file

  1. import { ChangeDetectorRef, Component, HostListener, ViewChild } from &#39;@angular/core&#39;;
  2. @Component({
  3. selector: &#39;app-root&#39;,
  4. templateUrl: &#39;./app.component.html&#39;,
  5. styleUrls: [&#39;./app.component.sass&#39;]
  6. })
  7. export class AppComponent {
  8. public coOffset: any;
  9. public winWidth: any;
  10. @HostListener(&#39;window:resize&#39;, [&#39;$event&#39;])
  11. onResize() {
  12. this.resized();
  13. }
  14. @ViewChild(&#39;myContainer&#39;) coRef: any;
  15. constructor(public cd: ChangeDetectorRef)
  16. {
  17. this.winWidth = window.innerWidth;
  18. }
  19. ngAfterViewInit (): void {
  20. this.coOffset = this.coRef.nativeElement.offsetWidth;
  21. this.cd.detectChanges();
  22. }
  23. resized(): void {
  24. this.coOffset = this.coRef.nativeElement.offsetWidth;
  25. this.cd.detectChanges();
  26. }
  27. }

Besides, please have a look at the link below to see the issue & solution visually: TestWise Replay | StackOverFlow QA - Angular: Get the correct width value of a div element when the browser width changes

huangapple
  • 本文由 发表于 2023年2月19日 19:33:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499833.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定