Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
163
rated 0 times [  169] [ 6]  / answers: 1 / hits: 23753  / 8 Years ago, fri, june 17, 2016, 12:00:00

I have component like below which is basically a popover:



import {Component, Input, ViewChild} from 'angular2/core'

declare var $: any;

@Component({
selector: 'popover',
template: `
<div id=temp [ngStyle]={'position':'absolute', 'z-index':'10000', 'top': y + 'px', left: x + 'px'}
[hidden]=hidden #temp>
<ng-content></ng-content>
</div>
`
})
export class Popover {

@ViewChild(temp) temp;

private hidden: boolean = true;
private y: number = 0;
private x: number = 0;

show(target, shiftx = 0, shifty = 0){
let position = $(target).offset();
this.x = position.left + shiftx;
this.y = position.top + shifty;
this.hidden = false;

console.log(#temp, this.temp.nativeElement.getBoundingClientRect()); //all 0s
console.log(temp id, document.getElementById('temp').getBoundingClientRect()); //all 0s
}

hide(){
this.hidden = true;
}
}


Inside the show() method I am trying to get the result of getBoundingClientRect() but its returning 0 for all properties but when I type in document.getElementById(temp).getBoundingClientRect() from Chrome's console I get proper result with actual values in the properties. Why the difference and what can I do to get the actual value from my component?


More From » typescript

 Answers
5

For some reason, the DOM was not updated right after it was shown so, a setTimeout e.g. 10 did the trick.


[#61732] Wednesday, June 15, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
eliza

Total Points: 732
Total Questions: 96
Total Answers: 86

Location: Guam
Member since Fri, Jun 18, 2021
3 Years ago
;