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

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

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

问题

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

test.component.html:

<div style="display:inline" #myContainer>
   <!-- 这只是一个示例 -->
   <div>
     <li>...</li>
     <li>...</li>
     <ng-container *ngIf="coOffset <= winWidth;">
       <li>...</li>
       <li>...</li>
     </ng-container>
   <div>
</div>

test.component.ts:

public coOffset: number;
public winWidth: any;
@ViewChild('myContainer') coRef: ElementRef;

onResize(event) {
  this.winWidth = window.innerWidth;
}

ngAfterViewInit (): void {
  this.coOffset = this.coRef.nativeElement.offsetWidth;
}

ngAfterViewChecked(): void {
  this.coOffset = this.coRef.nativeElement.offsetWidth;
  this.cd.detectChanges();
}
英文:

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:

&lt;div style=&quot;display:inline&quot; #myContainer&gt;
   //this is only an example
   &lt;div&gt;
     &lt;li&gt;...&lt;/li&gt;
     &lt;li&gt;...&lt;/li&gt;
     &lt;ng-container *ngIf=&quot;coOffset &lt;= winWidth;&quot;&gt;
       &lt;li&gt;...&lt;/li&gt;
       &lt;li&gt;...&lt;/li&gt;
     &lt;/ng-container&gt;
   &lt;div&gt;
&lt;/div&gt;

test.component.ts:

public coOffset: number;
public winWidth: any;
@ViewChild(&#39;myContainer&#39;) coRef: ElementRef;

onResize(event) {
  this.winWidth = window.innerWidth;
}

ngAfterViewInit (): void {
  this.coOffset = this.coRef.nativeElement.offsetWidth;
}

ngAfterViewChecked(): void {
  this.coOffset = this.coRef.nativeElement.offsetWidth;
  this.cd.detectChanges();
}

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文件的完整代码:

import { ChangeDetectorRef, Component, HostListener, ViewChild } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.sass']
})
export class AppComponent  {
  public coOffset: any;
  public winWidth: any;
  @HostListener('window:resize', ['$event'])
  onResize() {
    this.resized();
  }

  @ViewChild('myContainer') coRef: any;
  
  constructor(public cd: ChangeDetectorRef)
  {
    this.winWidth = window.innerWidth;
  }
  
  ngAfterViewInit (): void {
    this.coOffset = this.coRef.nativeElement.offsetWidth;
    this.cd.detectChanges();
  }

  resized(): void {
    this.coOffset = this.coRef.nativeElement.offsetWidth;
    this.cd.detectChanges();
  }
}

此外,请查看以下链接,以可视化地了解问题和解决方案: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

import {  ChangeDetectorRef, Component, HostListener, ViewChild } from &#39;@angular/core&#39;;

@Component({
  selector: &#39;app-root&#39;,
  templateUrl: &#39;./app.component.html&#39;,
  styleUrls: [&#39;./app.component.sass&#39;]
})
export class AppComponent  {
  public coOffset: any;
  public winWidth: any;
  @HostListener(&#39;window:resize&#39;, [&#39;$event&#39;])
  onResize() {
    this.resized();
  }

  @ViewChild(&#39;myContainer&#39;) coRef: any;
  
  constructor(public cd: ChangeDetectorRef)
  {
    this.winWidth = window.innerWidth;
  }
  
  ngAfterViewInit (): void {
    this.coOffset = this.coRef.nativeElement.offsetWidth;
    this.cd.detectChanges();
  }

  resized(): void {
    this.coOffset = this.coRef.nativeElement.offsetWidth;
    this.cd.detectChanges();
  }
}

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:

确定