【问题标题】:Window orientationchange event in AngularAngular中的窗口方向更改事件
【发布时间】:2014-04-10 14:49:05
【问题描述】:

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

我目前正在使用angular-masonry,我正在尝试更新/刷新 移动设备的方向发生变化时的砌体。

我找到了这个,但我想知道如何使用 Angular 中的 $window 服务来做到这一点

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

【问题讨论】:

    标签: angularjs angularjs-directive orientation-changes masonry


    【解决方案1】:
    angular.element($window).bind('orientationchange', function () {
    
    });
    

    【讨论】:

    • 谢谢,比我想象的要容易!
    • Ofc 不要忘记在你的控制器中注入 $window !
    • 但是有一些非常奇怪的事情,'orientationchange' 在屏幕宽度改变之前触发......所以如果我在'orientationchange' 之后执行某些操作并尝试获取新的宽度它不起作用,给我旧的。
    • 别忘了加上 $scope.$apply();
    【解决方案2】:

    希望这会有所帮助。将指令作为 attr 附加到正文。用 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});
                });
    
            }
        };
    }]);
    

    【讨论】:

    • 这个解决方案帮助我完成了我的地图指令。谢谢=D
    【解决方案3】:

    我是这样实现的:

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

    你怎么看?

    欢迎评论。

    【讨论】:

      猜你喜欢
      • 2012-02-04
      • 2018-04-13
      • 1970-01-01
      • 2014-09-06
      • 1970-01-01
      • 2014-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多