【问题标题】:Define and Watch a variable according to windowidth根据windowidth定义和观察一个变量
【发布时间】:2016-08-15 04:10:48
【问题描述】:

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

与使用 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/

【问题讨论】:

    标签: angularjs resize angular-directive


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢。我是菜鸟,所以请多多包涵。
    【解决方案2】:

    无需手表,您已经绑定到调整大小。把你的逻辑移到那里。正如段君所说,你不断地创造功能。变化如下:

    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/

    【讨论】:

      【解决方案3】:

      正如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>

      【讨论】:

      • 太棒了。谢谢。我也很欣赏 //cmets
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-22
      • 2018-04-18
      • 1970-01-01
      • 1970-01-01
      • 2020-01-30
      • 1970-01-01
      • 2023-01-13
      相关资源
      最近更新 更多