Şablondaki div boyutu değiştirdiğinde bazı eylemleri gerçekleştirmenin en iyi yolu nedir? Pencere yeniden boyutlandırıldığında divun boyutu değişir. Rxjs ile Gözlemlenebilir/abone olun veya başka bir yol var mı?Öğe boyutu değiştiğinde nasıl gözlenir Angular 2
Şablon:
<div #eMainFrame class="main-frame">
...
</div>
Bileşen:
@Component({
selector: 'app-box',
templateUrl: './box.component.html',
styleUrls: ['./box.component.css']
})
export class BoxComponent implements OnInit {
@ViewChild('eMainFrame') eMainFrame : ElementRef;
constructor(){}
ngOnInit(){
// This shows the elements current size
console.log(this.eMainFrame.nativeElement.offsetWidth);
}
}
Güncelleme bileşen (bu örnek pencere boyutu değiştirildiğinde algılar)
constructor(ngZone: NgZone) {
window.onresize = (e) => {
ngZone.run(() => {
clearTimeout(this.timerWindowResize);
this.timerWindowResize = setTimeout(this._calculateDivSize, 100);
});
}
}
_calculateDivSize(){
console.log(this.eMainFrame.nativeElement.offsetWidth);
}
ama bu bana bir hata veriyor :
EXCEPTION: Cannot read property 'nativeElement' of undefined
kullanın: Bu çeki yapmak için iyi bir yer olduğunu düşünüyorum? Bu yardımcı olabilir: http://stackoverflow.com/questions/21170607/angularjs-bind-to-directive-resize – Rajesh