Angular 升级和 jQuery 问题

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

Angular upgrade and jquery issue

问题

作为角部分6到15迁移的一部分,我必须更新jQuery。之前我们使用了

"@types/jquery": "2.0.33" 
"jquery": "3.1.1" 

作为package.json的一部分,由于我们升级到了angular,我们的jQuery如下:

"jquery": "^3.6.4",
"@types/jquery": "^3.5.16"

在某个地方,我们在.ts文件中使用了以下查询函数:

this.cssService.setElHeight($('.div .content h4'));

以下是服务方法:

setElHeight(element: any) {
        element.height('auto');
        if (element.length) {
            let maxheight: number = 0;
            element.each(() => {
             
              let max_height = $(this).height() ?? 0; //这里我得到错误 无法读取未定义的属性(读取'defaultView')
              maxheight = (max_height > maxheight ? max_height : maxheight);
            });
            element.height(maxheight);
        }
    }

在" let max_height = $(this).height() ?? 0;"这一行,我们得到以下错误:

"无法读取未定义的属性(读取'defaultView')"

我尝试过用$(element)替换了$(this),但有些内容的高度不正确。

英文:

As part of angular 6 to 15 migration, I have to update jquery as well. earlier we have used

"@types/jquery": "2.0.33" 
"jquery": "3.1.1" 

As part of package.json, since we have upgraded to angular, we have jquery as below:

"jquery": "^3.6.4",
"@types/jquery": "^3.5.16"

In one of the places we have used the below query function in the .ts file

this.cssService.setElHeight($('.div .content h4'));

Below is the service method:

setElHeight(element: any) {
        element.height('auto');
        if (element.length) {
            let maxheight: number = 0;
            element.each(() => {
             
              let max_height = $(this).height() ?? 0; //here I get error  Cannot read properties of undefined (reading 'defaultView')
              maxheight = (max_height > maxheight ? max_height : maxheight);
            });
            element.height(maxheight);
        }
    }

at line " let max_height = $(this).height() ?? 0;" we get error as below:

" Cannot read properties of undefined (reading 'defaultView')"

Instead of $(this) I tried $(element), but some of the content don't get proper height.

答案1

得分: 1

避免直接使用jQuery或任何其他外部库来操作DOM。Angular提供了自己的方式来与DOM交互,使用ElementRef和Renderer2。

从@angular/core导入ElementRef和Renderer2:

import { ElementRef, Renderer2 } from '@angular/core';

将ElementRef和Renderer2注入到你的服务构造函数中:

constructor(private renderer: Renderer2) {}

修改你的setElHeight方法,使用Renderer2而不是jQuery:

setElHeight(element: ElementRef) {
  const el = element.nativeElement;
  this.renderer.setStyle(el, 'height', 'auto');
  if (el.length) {
    let maxheight: number = 0;
    element.forEach((el: any) => {
      const max_height = el.offsetHeight || 0;
      maxheight = (max_height > maxheight ? max_height : maxheight);
    });
    this.renderer.setStyle(el, 'height', maxheight + 'px');
  }
}

更新你的模板,将ElementRef传递给setElHeight方法:

<div #content class="div content">
  <h4>Some Content</h4>
</div>

希望它完美工作。

使用jQuery

setElHeight(element: any) {
  element.height('auto');
  if (element.length) {
    let maxheight: number = 0;
    element.each(function() {
      const max_height = $(this).height() || 0;
      maxheight = (max_height > maxheight ? max_height : maxheight);
    });
    element.height(maxheight);
  }
}

这是你要的翻译部分。

英文:

Avoid directly manipulating the DOM using jQuery or any other external library. Angular provides its own way of interacting with the DOM using ElementRef and Renderer2.

Import ElementRef and Renderer2 from @angular/core:

import { ElementRef, Renderer2 } from &#39;@angular/core&#39;;

Inject ElementRef and Renderer2 into your service's constructor:

constructor(private renderer: Renderer2) {}

Modify your setElHeight method to use Renderer2 instead of jQuery:

setElHeight(element: ElementRef) {
  const el = element.nativeElement;
  this.renderer.setStyle(el, &#39;height&#39;, &#39;auto&#39;);
  if (el.length) {
    let maxheight: number = 0;
    element.forEach((el: any) =&gt; {
      const max_height = el.offsetHeight || 0;
      maxheight = (max_height &gt; maxheight ? max_height : maxheight);
    });
    this.renderer.setStyle(el, &#39;height&#39;, maxheight + &#39;px&#39;);
  }
}

Update your template to pass the ElementRef to the setElHeight method

&lt;div #content class=&quot;div content&quot;&gt;
  &lt;h4&gt;Some Content&lt;/h4&gt;
&lt;/div&gt;

hope it's work perfectly

Using JQuery

setElHeight(element: any) {	
  element.height(&#39;auto&#39;);	
  if (element.length) {	
    let maxheight: number = 0;	
    element.each(function() {	
      const max_height = $(this).height() || 0;	
      maxheight = (max_height &gt; maxheight ? max_height : maxheight);	
    });	
    element.height(maxheight);	
  }	
}

答案2

得分: 0

我已经让它按照以下方式运行对于有相同需求的人可能会有用

     setElHeight(element: any) {
            element.height('auto');
            var $elem = $(element)
            if ($elem.length) {
                let maxheight: number = 0;
                $elem.each(function(key, value)  {
                    const max_height = $(this).height() || 0; 
                    maxheight = (max_height > maxheight ? max_height : maxheight);
                });
                element.height(maxheight);
            }
        }
英文:

I have got it working following way: it might be useful to someone who is in same need:

 setElHeight(element: any) {
        element.height(&#39;auto&#39;);
        var $elem = $(element)
        if ($elem.length) {
            let maxheight: number = 0;
            $elem.each(function(key, value)  {
                const max_height = $(this).height() || 0; 
                maxheight = (max_height &gt; maxheight ? max_height : maxheight);
            });
            element.height(maxheight);
        }
    }

huangapple
  • 本文由 发表于 2023年5月22日 16:06:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76304155.html
匿名

发表评论

匿名网友

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

确定