angular - 屏幕方向更改更新文本
问题描述
看法:
<Label [text]="name"></Label>
零件:
import { Component } from '@angular/core'
import * as screenOrientation from '@nativescript/core/application';
@Component({
selector: 'ns-new',
templateUrl: './new.component.html',
styleUrls: ['./new.component.css'],
})
export class NewComponent {
name:string = "Not Rotated";
constructor( ) {
screenOrientation.on("orientationChanged", this.onOrientationChanged)
}
onOrientationChanged = (evt) => {
console.log("old name " + this.name);
this.name="Rotated";
console.log("new name " + this.name);
}
}
当屏幕旋转时,文本不会改变。
- 屏幕旋转
- 代码被执行
- 但是标签中的文本不会改变。
有谁知道我错过了什么,或者问题出在哪里?
解决方案
我认为问题在于,当您在“外部”Angular 进行更改时,您需要指示 Angular 发生了更改(*)。为此,您需要使用 ngZone
//in constructor you inject ngZone
constructor(private ngZone: NgZone) { }
//when a change outside Angular happens
onOrientationChanged(evt) => {
//this line happens outside Angular
console.log("old name " + this.name);
this.ngZone.run(() => { //but these another run in Angular
this.name="Rotated";
console.log("new name " + this.name);
})
})
(*) 典型情况是 javaScript 中的调用或 Cordova 中的事件
推荐阅读
- php - 碳 - 日期采用 ISO8601 格式,带有偏移表示法
- apache-spark - 分区中的火花模式差异
- c# - 1 个表的 3 个外键
- sql-server - 从 Azure DevOps Pipeline 访问 SQL Server
- python - 将在 CPanel 中创建 python 应用程序的终端代码
- python - 为什么数字不正确?
- layout - Shopify 模板“如果标签从未关闭”
- flutter - 如何在 FirebaseStorage 中使用 StreamBuilder 来通知我的应用我的 Firebase 存储发生变化?在颤振中
- php - Laravel Blade 组件方法在生产中未定义但在本地开发中工作
- git - 我可以在结帐后恢复分阶段的更改吗?