如何在控制器调整大小时获得angularJS中的窗口宽度?

Hol*_*lly 19 angularjs

我可以通过控制器调整大小来获得angularJS中的窗口宽度吗?我希望能够得到它,所以我可以显示一些div<div ng-if="windowWidth > 320">

我可以在初始页面加载时获取windowWidth,但不能在调整大小时...

'use strict';

var app = angular.module('app', []);

app.controller('mainController', ['$window', '$scope', function($window, $scope){
	var mainCtrl = this;
	mainCtrl.test = 'testing mainController';
  
    // Method suggested in @Baconbeastnz's answer  
    $(window).resize(function() {
      $scope.$apply(function() {
        $scope.windowWidth = $( window ).width();
      });
    });
  
    /* this produces the following error
    /* Uncaught TypeError: mainCtrl.$digest is not a function(…)
    angular.element($window).bind('resize', function(){
        mainCtrl.windowWidth  = $window.innerWidth;
        // manuall $digest required as resize event
        // is outside of angular
        mainCtrl.$digest();
    });  
    */
}]);

// Trying Directive method as suggested in @Yaser Adel Mehraban answer.
/*app.directive('myDirective', ['$window', function ($window) {
     return {
        link: link,
        restrict: 'E'            
     };
     function link(scope, element, attrs){
       angular.element($window).bind('resize', function(){
           scope.windowWidth = $window.innerWidth;
       });    
     }    
 }]);*/
Run Code Online (Sandbox Code Playgroud)
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.9/angular.min.js"></script>
<body ng-app="app" ng-controller="mainController as mainCtrl">
	<p>{{mainCtrl.test}}</p>
	<hr />
    <p ng-if="windowWidth > 600">The window width is {{windowWidth}}</p>
    <div my-directive ng-if="windowWidth > 320">It works!</div>
</body>
Run Code Online (Sandbox Code Playgroud)

我在这个答案中看到他们解释了如何从指令中获取它,但是如何从控制器中获取它呢?

Yas*_*ser 22

最好的方法是使用指令并观察窗口的resize事件:

'use strict';

var app = angular.module('plunker', []);

app.directive('myDirective', ['$window', function ($window) {

     return {
        link: link,
        restrict: 'A'           
     };

     function link(scope, element, attrs){

       angular.element($window).bind('resize', function(){
           scope.windowWidth = $window.innerWidth;
       });    
     }    
 }]);
Run Code Online (Sandbox Code Playgroud)

并在你的div上使用它:

<div my-directive ng-if="windowWidth > 320">
Run Code Online (Sandbox Code Playgroud)

这是一个工作的plunker.


Hol*_*lly 7

最终得到它与下面的工作.从/sf/answers/1615472981/获取大部分代码.

唯一的区别是ng-if工作指令必须放在父html元素上.

'use strict';

var app = angular.module('app', []);

app.controller('mainController', ['$window', '$scope', function($window, $scope){
	var mainCtrl = this;
	mainCtrl.test = 'testing mainController';
}]);

app.directive('windowSize', function ($window) {
  return function (scope, element) {
    var w = angular.element($window);
    scope.getWindowDimensions = function () {
        return {
            'h': w.height(),
            'w': w.width()
        };
    };
    scope.$watch(scope.getWindowDimensions, function (newValue, oldValue) {
      scope.windowHeight = newValue.h;
      scope.windowWidth = newValue.w;
      scope.style = function () {
          return {
              'height': (newValue.h - 100) + 'px',
              'width': (newValue.w - 100) + 'px'
          };
      };
    }, true);

    w.bind('resize', function () {
        scope.$apply();
    });
  }
})
Run Code Online (Sandbox Code Playgroud)
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.9/angular.min.js"></script>
<body window-size my-directive ng-app="app" ng-controller="mainController as mainCtrl">
  <p>{{mainCtrl.test}}</p>
  <hr />
  <div ng-if="windowWidth > 500">
    <h4 style="margin:5px 0">It works!</h4>
    <p style="margin:0">window.height: {{windowHeight}}</p>			     <p style="margin:0">window.width: {{windowWidth}}</p>			     <p style="margin:0">{{mainCtrl.test}}</p>
  </div>
</body>
Run Code Online (Sandbox Code Playgroud)


Bac*_*tnz 6

在调整大小时获取窗口宽度并不是Angular JS特有的.事实上,Angular没有提供任何能力.它是本机javascript,本机窗口对象会触发您可以访问的resize事件.jQuery为此提供了一个方便的包装器.通过在控制器中使用简单的回调,然后更新$ scope对象上的双重绑定windowWidth属性,您可以在不使用指令的情况下获得所需的功能.

$(window).resize(function() {
  $scope.windowWidth = $( window ).width();
});
Run Code Online (Sandbox Code Playgroud)