Angularjs 角度窗口方向更改事件

Angularjs 角度窗口方向更改事件,angularjs,angularjs-directive,orientation-changes,masonry,Angularjs,Angularjs Directive,Orientation Changes,Masonry,有没有办法在AngularJS指令中调用orientationchange事件 我目前正在使用,我正在尝试更新/刷新 当移动设备的方向发生变化时 我发现了这个,但我想知道如何使用内部的$window服务来实现这一点 window.addEventListener("orientationchange", function() { // Announce the new orientation number console.log(window.orientation); }, false

有没有办法在AngularJS指令中调用orientationchange事件

我目前正在使用,我正在尝试更新/刷新 当移动设备的方向发生变化时

我发现了这个,但我想知道如何使用内部的$window服务来实现这一点

window.addEventListener("orientationchange", function() {
  // Announce the new orientation number
  console.log(window.orientation);
}, false);

我是这样实施的:

$window.addEventListener('orientationchange',function(){
你的代码在这里
},假);
你觉得怎么样


欢迎评论。

希望这有帮助。将指令作为attr附加到正文。用iphone5测试

'use strict';

angular.module('appModuleNameHere')
    .directive('DirPrefixNameOrientationChange', ['$rootScope',function ($state) {
    return {
        restrict: 'A',
        replace: true,
        link: function postLink($scope, element, attrs) {

            element.bind('orientationchange', function () {
                $state.go($state.current, {}, {reload: true});
            });

        }
    };
}]);

Thx,比我想象的容易!Ofc别忘了在控制器中注入$window!但是有一件非常奇怪的事情,在屏幕宽度更改之前,“方向更改”会触发…因此,如果我在“方向更改”之后执行某个操作,并尝试获取新的宽度,但它不起作用,请给我旧的宽度。别忘了添加$scope。$apply();这个解决方案在我的map指令中帮助了我。Thx=D
'use strict';

angular.module('appModuleNameHere')
    .directive('DirPrefixNameOrientationChange', ['$rootScope',function ($state) {
    return {
        restrict: 'A',
        replace: true,
        link: function postLink($scope, element, attrs) {

            element.bind('orientationchange', function () {
                $state.go($state.current, {}, {reload: true});
            });

        }
    };
}]);