根据窗口宽度

时间:2016-04-21 19:07:18

标签: angularjs resize angular-directive

我正在努力创建一个指令来分配和更新变量,与窗口宽度进行比较,并使用resize进行更新。

与使用CSS相比,我需要变量,因为我会将其用于ng-if。我究竟做错了什么?这是代码:

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

function AppController($ scope){}

app.directive('widthCheck', function ($window) {
    return function (scope, element, attr) {

        var w = angular.element($window);
        scope.$watch(function () {
            return {
                'w': window.innerWidth
            };
        }, function (newValue, oldValue, desktopPlus, isMobile) {
            scope.windowWidth = newValue.w;
            scope.desktopPlus = false;
            scope.isMobile = false;
            scope.widthCheck = function (windowWidth, desktopPlus) {
                if (windowWidth > 1399) {
               scope.desktopPlus = true;
              }
              else if (windowWidth < 769) {
                scope.isMobile = true;
              }
              else {
                scope.desktopPlus = false;
                scope.isMoblie = false;
              }
            }

        }, true);

        w.bind('resize', function () {
            scope.$apply();
        });
    }
}); 

JS在这里:http://jsfiddle.net/h8m4eaem/2/

3 个答案:

答案 0 :(得分:0)

每次此观察者触发时,

scope.widthCheck都会被分配一个匿名函数并重新分配相同的函数。我也注意到它从未被召唤过。

你应该将这件作品从$ watch中移出并在观察者触发时调用该函数

答案 1 :(得分:0)

不需要手表,你已经绑定调整大小。只需将逻辑移到那里。而Jun Duan说你不断创造这个功能。这是改变:

app.directive('widthCheck', function ($window) {
    return function (scope, element, attr) {

        function widthCheck(windowWidth) {
            if (windowWidth > 1399) {
                scope.desktopPlus = true;
            }
            else if (windowWidth < 769) {
                scope.isMobile = true;
            }
            else {
                scope.desktopPlus = false;
                scope.isMoblie = false;
            }
        }   
        windowSizeChanged();

        angular.element($window).bind('resize', function () {
            windowSizeChanged();            
            scope.$apply();
        });
        function windowSizeChanged(){
            scope.windowWidth = $window.innerWidth;
            scope.desktopPlus = false;
            scope.isMobile = false;
            widthCheck(scope.windowWidth);
        }
    }
}); 

jsFiddle:http://jsfiddle.net/eqd3zu0j/

答案 2 :(得分:0)

正如本SO answer中所提到的,最好不用手表来绑定窗口调整大小事件。 (类似于伯格先生的回答。)

下面的演示或此fiddle中的内容应该有效。

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

function AppController($scope) {}

app.directive('widthCheck', function($window) {
  return function(scope, element, attr) {
    var width, detectFalse = {
      desktopPlus: false,
      isTablet: false,
      isMobile: false
    };

    scope.windowWidth = $window.innerWidth;

    checkSize(scope.windowWidth); // first run

    //scope.desktopPlus = false;
    //scope.isMoblie = false; // typo
    //scope.isTablet = false;
    //scope.isMobile = false;

    function resetDetection() {
      return angular.copy(detectFalse);
    }

    function checkSize(windowWidth) {
      scope.detection = resetDetection();

      if (windowWidth > 1000) { //1399) {
        scope.detection.desktopPlus = true;
      } else if (windowWidth > 600) {
        scope.detection.isTablet = true;
      } else {
        scope.detection.isMobile = true;
      }
    }
    angular.element($window).bind('resize', function() {
      width = $window.innerWidth;
      scope.windowWidth = width

      checkSize(width);
      // manuall $digest required as resize event
      // is outside of angular
      scope.$digest();
    });
  }
});
.fess {
  border: 1px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="miniapp" ng-controller="AppController">

  <div width-check class="fess" resize>
    window.width: {{windowWidth}}
    <br />desktop plus: {{detection.desktopPlus}}
    <br />mobile: {{detection.isMobile}}
    <br />tablet: {{detection.isTablet}}
    <br/>
  </div>
</div>