根据 windowidth 定义并观察一个变量

Define and Watch a variable according to windowidth

我正在努力创建一个指令来分配和更新一个变量,该变量与 window 宽度相比,并通过调整大小进行更新。

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

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

函数 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();
        });
    }
}); 

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

scope.widthCheck 被分配了一个匿名函数,并在每次此观察者触发时重新分配相同的函数。我还注意到它从未被调用过。

你应该把那块移出 $watch 并在 watcher 触发时调用函数

无需手表,您已经绑定调整大小。只需将您的逻辑移到那里即可。正如段俊所说,你不断地创造功能。这是变化:

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/

如本 中所述,最好在不监视的情况下绑定到 window 调整大小事件。 (类似于伯格先生的回答。)

像下面的演示或这个 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>