我想使用 elementRef 移动组件的位置。 我下面的代码看起来相当合理 - 但不起作用。为什么?
ngAfterViewInit() {
let element = this.elementRef.nativeElement
element.style.color = "red" // WORKS!
element.style.top = "100px" // NO EFFECT!
}
这是我的示例的链接:https://stackblitz.com/edit/angular-qlzkkc?file=app%2Fhello.component.ts
最佳答案
这是CSS问题。例如,如果添加 element.style.position = "absolute"
,您将看到 top = "100px"
有效。
关于css - elementRef.style.top = "100px"不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50142454/