前提・実現したいこと
Angularを使い、addEventListenerの値をビューに更新するプログラムを作っています。
発生している問題・エラーメッセージ
以下ソースコードにて、x,y,zの値が更新されない(初期値で設定した0のまま)です。
試行錯誤の上、試したことに記載した内容で更新されることを確認したのですが、
以下ソースにて更新されない理由がわからなく、ご存知の方、ご教授いただきたいです。
該当のソースコード
typescript
1import { Component, OnInit } from '@angular/core'; 2 3@Component({ 4 selector: 'app-root', 5 templateUrl: './app.component.html', 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 15 ngOnInit(){ 16 this.func2(); 17 } 18 19 func1():void{ 20 setInterval(() =>{ 21 this.func2(); 22 }, 1000); 23 } 24 25 func2():void{ 26 window.addEventListener('devicemotion', (event) => { 27 this.x = event.accelerationIncludingGravity.x; 28 this.y = event.accelerationIncludingGravity.y; 29 this.z = event.accelerationIncludingGravity.z; 30 }); 31 } 32} 33
試したこと
ngOnInit()関数を以下のようにすると、更新されるようになりました。
ngOnInit(){ this.func1(); }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。