【问题标题】:Rotate an image in Angular 9在Angular 9中旋转图像
【发布时间】:2020-06-29 11:59:37
【问题描述】:

我需要从我的组件中旋转 html 中的图像

(document.querySelector('#image') as HTMLElement).style.transform = `rotate(${degree}deg)`;

document.getElementById('#image').style.transform = `rotate(${degree}deg)`;

并在此处为其设置一个 id

<img id= "image" class="arrow" src = "assets/arrow.png"/>

但它使页面崩溃并出现此错误

core.js:5882 ERROR TypeError: Cannot read property 'style' of null

【问题讨论】:

  • 当你需要旋转元素时添加一个类怎么样?类是使用旋转样式预定义的。是否满足您的要求?
  • @SandyB 问题是我需要计算角度然后更新组件构造函数内的图像旋转,但它崩溃了
  • 堆叠闪电战怎么样?您可以设置它以重现问题
  • @SandyB 我在问上面的代码是否正确?我搜索了很多,很奇怪没有人知道图像存在于 html 中的问题,当我在 html 中设置样式但无法从组件中旋转时,它会旋转

标签: html angular web rotation


【解决方案1】:

不带井号的引用它并使用renderer2 来编辑样式/类。

详情请查看我的stackblitz sample,主要变化如下:

  constructor(private renderer: Renderer2) {}

  ngOnInit() {
    const degree = 90;
    const image = document.getElementById('image');
    this.renderer.setStyle(
      image,
      'transform',
      `rotate(${degree}deg)`
    )
  }

【讨论】:

猜你喜欢
  • 2013-08-26
  • 1970-01-01
  • 1970-01-01
  • 2017-10-06
  • 2021-08-19
  • 2013-10-10
  • 2021-02-05
  • 2018-10-14
  • 2013-11-04
相关资源
最近更新 更多