简体   繁体   English

角度变化检测,在视图更新后根据 svg 对象几何属性更新视图的正确方法

[英]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 :我想做的是与文本对象重叠管理有关,简而言之:

  1. Get a list of objects with a date and a text description from the backend.从后端获取带有日期和文本描述的对象列表。
  2. These objects should display on an SVG timeline, the horizontal position is based on the date.这些对象应显示在 SVG 时间轴上,水平位置基于日期。
  3. Once the view is updated, I want to set a top attribute on each text object based on the width property to detect if two objects overlap.视图更新后,我想根据 width 属性在每个文本对象上设置一个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.

相关问题 根据角度函数2的输出更改视图属性 - Change view properties based on output of function angular 2 有没有办法在 Angular 中触发更改检测以在删除元素时更新视图中的数组? - Is there a way to trigger change detection in Angular to update an array in the view when an element is removed? 订阅中的变量更改后,Angular 2 View 不会更新 - Angular 2 View will not update after variable change in subscribe Angular 9 - 订阅中的变量更改后视图不会更新 - Angular 9 - View will not update after variable change in subscribe Angular2中的更改未更新视图 - View is not updated on change in Angular2 Angular 2:数组更新后视图不会更新 - Angular 2 : View doesn`t update after array is updated 从服务器调用angular 4更新对象后,视图未更新 - view is not being updated after updating object from server call angular 4 在模型更改后的视图更新后的Angular 2 Callback - Angular 2 Callback after view update after a change in a model Angular - 基于服务更新视图的最佳方法(性能) - Angular - best way (for performance) to update view based on service Angular 2+:更改检测,属性更改和视图更新之间的状态 - Angular 2+: change detection, the state between property-change and view-update
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM