[英]Angular change detection, proper way to update a view based on svg object geometrical properties after the view is updated
I am not sure if there is a proper way of doing this in Angular...我不确定在 Angular 中是否有正确的方法来做到这一点......
What I want to do is related to text objects overlap management, in short :我想做的是与文本对象重叠管理有关,简而言之:
top
attribute on each text object based on the width property to detect if two objects overlap.top
属性,以检测两个对象是否重叠。 The way I am doing this now :我现在这样做的方式:
In the ngAfterViewChecked
, I am checking the this.svgObj.nativeElement.children['textObjId'].textLength.baseVal.value
.在
ngAfterViewChecked
,我正在检查this.svgObj.nativeElement.children['textObjId'].textLength.baseVal.value
。 I am using this width property to determine if two text objects overlap.我使用这个宽度属性来确定两个文本对象是否重叠。
My problem:我的问题:
Obviously this is raising a ExpressionChangedAfterItHasBeenCheckedError
as I am changing the top property after the view has been checked...显然,这是引发
ExpressionChangedAfterItHasBeenCheckedError
因为我正在检查视图后更改 top 属性...
Is there any other "angular way" of achieving what I am trying to do ?有没有其他“角度方式”来实现我想要做的事情?
I found my mistake and sharing here in case that helps.我发现了我的错误并在这里分享以防万一。
My problem : I was setting a property on my objects based on the dimensions of my SVG element after it was drawn.我的问题:我在绘制后根据我的 SVG 元素的尺寸在我的对象上设置一个属性。 Then binding that calculated property to some positioning property of my SVG element in my template.
然后将该计算属性绑定到我的模板中我的 SVG 元素的某些定位属性。 That was raising the ExpressionChangedAfterItHasBeenCheckedError.
这引发了 ExpressionChangedAfterItHasBeenCheckedError。
Solution : Setting the SVG position property directly from the ngAfterViewChecked method by accessing the element from there.解决方案:通过从那里访问元素,直接从 ngAfterViewChecked 方法设置 SVG 位置属性。 Something like that :
类似的东西:
export class SampleComponent implements AfterViewChecked {
@ViewChild('sampleSVG') sampleSVG: ElementRef;
ngAfterViewChecked(): void {
this.sampleSVG.nativeElement.children['element-id-123'].y.baseVal.value = 12345;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.