質問
以下ソースコードにて、
addEventListenerを使ったAngularのビュー更新をしようとしております。
しかし、表示を確認すると、xの値が初期値で設定した0から更新されません。
func2関数にてxの値を変更しているのに、表示上更新されていないことの理由をご教授いただきたいです。
typescript
1import { Component, OnInit } from '@angular/core'; 2 3@Component({ 4 selector: 'app-root', 5 template: '<p>{{x}}</p>', 6 styleUrls: ['./app.component.scss'] 7}) 8 9export class AppComponent implements OnInit{ 10 x:any = 0; 11 y:any = 0; 12 z:any = 0; 13 14 ngOnInit(){ 15 this.func2(); 16 } 17 18 func1():void{ 19 setInterval(() =>{ 20 this.func2(); 21 }, 1000); 22 } 23 24 func2():void{ 25 window.addEventListener('devicemotion', (event) => { 26 this.x = event.accelerationIncludingGravity.x; 27 this.y = event.accelerationIncludingGravity.y; 28 this.z = event.accelerationIncludingGravity.z; 29 }); 30 } 31} 32
試したこと
ngOnInit関数を以下のように変更すると、
値が更新されていました。
ただ、上記質問でも記載させていただきました通り、
上記のソースコードにて表示の更新がされないことについてご教授いただきたいです。
ngOnInit(){ this.func1(); }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/05/11 15:51