9

有没有办法在AngularJS指令中调用orientationchange事件?Angular中的window orientationchange事件

我目前与angular-masonry一起工作,我正在尝试在移动设备的方向更改时更新/刷新 砌体。

我发现这一点,但我不知道如何与内部角

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

回答

30
angular.element($window).bind('orientationchange', function() { 

}); 
+1

Thx,比我想象的要容易! – tdhulster

+3

Ofc不要忘记在控制器中注入$ window! – amdev

+0

但有些东西很奇怪,'orientationchange'在屏幕宽度改变之前就是火了......所以如果我在'orientationchange'之后执行一些东西,并试图获得新的宽度,它不起作用,请给我旧的。 – amdev

3
的$窗口服务做到这一点

我实现了这种方式:

$window.addEventListener('orientationchange', function() { 
    your code here 
}, false); 

做什么您认为?

欢迎评论。

6

希望这会有所帮助。将指令作为attr附加到body。使用iPhone 5测试。

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

     } 
    }; 
}]); 
+0

这个解决方案在我的map指令中帮了我很大的忙。Thx = D –

相关问题