更改手机方向,但范围变量不刷新 [英] change phone orientation but scope variable doesn't refresh

查看:85
本文介绍了更改手机方向,但范围变量不刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用cordova插件来处理方向.如果方向发生变化,我想更改为其他视图.因此,我尝试在$ scope中定义一个变量,并在UI上进行相关更改.这是我的代码

I am using cordova plugin to handle orientation. I want to change to a different view if orientation changes. So I try to define a variable in $scope and do related changes on UI. here is my code

window.addEventListener("orientationchange", function() { 
    $scope.orientation = screen.orientation;
});

我从cordova插件定位首页中引用的此代码段.但方向改变时,UI不会发生任何变化.我什至没有定义$ scope.orientation(我通过ng-show测试了)

this code snippet I referred from cordova plugin orientation homepage. but the UI doesn't have any change when orientation changes. I even got $scope.orientation undefined(I tested by ng-show)

这是怎么发生的?感谢您的帮助.

How did this happen? Thanks for any help.

推荐答案

使用$apply().

angular.element($window).on("orientationchange", function() { 
    $scope.orientation = screen.orientation;
    $scope.$apply();
});

这使AngularJS框架知道发生了更改.

This lets the AngularJS framework know a change has occurred.

这篇关于更改手机方向,但范围变量不刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆