Angular中的Window orientationchange事件

tdh*_*ter 11 orientation-changes angularjs angularjs-directive masonry

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

我正在使用角度砌体,当移动设备的方向发生变化时,我正在尝试更新/刷新砌体.

我发现了这个,但我想知道如何使用Angular中的$ window服务

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

小智 31

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

});
Run Code Online (Sandbox Code Playgroud)

  • Ofc不要忘记在你的控制器中注入$ window! (3认同)

Nic*_*ras 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});
            });

        }
    };
}]);
Run Code Online (Sandbox Code Playgroud)