change phone orientation but scope variable doesn&

2019-09-09 20:50发布

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条回答
相关推荐>>
2楼-- · 2019-09-09 21:09

Use $apply().

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

This lets the AngularJS framework know a change has occurred.

查看更多
登录 后发表回答