窗口调整角度的信息

spa*_*acm 4 javascript window-resize angularjs

我刚刚发布这个小提琴,认为它可能对像我这样的人有帮助.它显示了如何将普通的javascript传递给角度范围.在这种情况下,范围获取窗口内部化信息.

http://jsfiddle.net/spacm/HeMZP/

为了个人使用,我将传递给角度范围这些信息:

  • 宽度和高度
  • 方向/方向变化
  • iframe检测
  • OS检测

使用navigator.platform变量

function isInIFrame(){
    return window.location !== window.parent.location;
}

function updateMediaInfoWH() {
    if((typeof(mediaInfo.inIFrame)!='undefined') && (!mediaInfo.inIFrame)) {
        mediaInfo.width = innerWidth;
        mediaInfo.height = innerHeight;
        updateMediaInfoOrientation();
    }
    tellAngular();
}

function tellAngular() {
    console.log("tellAngular");
    var domElt = document.getElementById('mainContainer');
    scope = angular.element(domElt).scope();
    console.log(scope);
    scope.$apply(function(){
        scope.mediaInfo = mediaInfo;
        scope.info = mediaInfo.width;
    });
}
Run Code Online (Sandbox Code Playgroud)

欢迎任何评论.

Wes*_*aro 6

我没有看到要回答的问题,所以我假设您的问题正在寻找您的想法的输入.

使用Angular可能与您正常工作的方式非常不同,主要是因为他们完全和完全使用依赖注入.

您不应该"告诉"Angular任何东西,您应该能够通过注入的依赖项Angular服务访问所有这些信息.

使用你向我展示的东西,它看起来像这样:

my.MediaInfo = function($window) {
  this.window_ = $window;
  this.inIframe = $window.self !== $window.top;
  if(!this.inIFrame) {
    this.width = $window.innerWidth;
    this.height = $window.innerHeight;
  } else {
    this.width = this.height = ...;
  }
}

my.angular.controller = function($scope, mediaInfo) {
  // {width: X, height: Y, inIframe: false}
  $scope.mediaInfo = mediaInfo;
}
Run Code Online (Sandbox Code Playgroud)

然后你的Angular模块看起来像:

angular.module('module', [])
    .service('mediaInfo', my.MediaInfo)
    .controller('mainCtrl', my.angular.controller);
Run Code Online (Sandbox Code Playgroud)

希望这能很好地证明你应该如何将数据导入Angular.