change phone orientation but scope variable doesn't refresh

无人久伴 提交于 2019-12-12 01:27:03

问题


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;
});

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.


回答1:


Use $apply().

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

This lets the AngularJS framework know a change has occurred.



来源:https://stackoverflow.com/questions/35986921/change-phone-orientation-but-scope-variable-doesnt-refresh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!