简体   繁体   English

角度调整窗口大小的信息

[英]window resizing info in angular

I just published this fiddle thinking it could be helpful for people like me. 我只是发表了这种小提琴,认为它可能对像我这样的人有所帮助。 It shows how plain javascript can be passed to an angular scope. 它显示了如何将普通的javascript传递给角度范围。 In this case, the scope gets window innersize information. 在这种情况下,范围将获取窗口内部尺寸信息。

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

For personal use, I'll pass to angular scope these informations: 对于个人用途,我将这些信息传递给角度范围:

  • width & heigth 宽度和高度
  • orientation / orientation change 方向/方向改变
  • iframe detection iframe检测
  • OS detection 操作系统检测

using the navigator.platform variable 使用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;
    });
}

Any comment is welcome. 欢迎发表任何评论。

I don't see a question to answer, so I'll assume your question is looking for input on your idea. 我看不到要回答的问题,因此我假设您的问题正在寻找您的想法的输入。

Getting going with Angular can be very different than the way you normally work, mainly because of their complete and total use of dependency injection. 开始使用Angular可能与您通常的工作方式大不相同,主要是因为它们完全并完全使用了依赖项注入。

You shouldn't have to "tell" Angular anything, you should be able to access all of this information through an injected dependency, an Angular service. 您不必“告诉” Angular任何东西,您应该能够通过注入的依赖项Angular服务访问所有这些信息。

Using what you've shown me, it could look something like this: 使用您显示给我的内容,它看起来可能像这样:

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

Then your Angular module would look like: 然后,您的Angular模块将如下所示:

angular.module('module', [])
    .service('mediaInfo', my.MediaInfo)
    .controller('mainCtrl', my.angular.controller);

Hopefully, this is a good demonstration of how you should be getting data into Angular. 希望这很好地演示了如何将数据导入Angular。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM