AngularJS $watch window resize inside directive

前端 未结 3 1275
借酒劲吻你
借酒劲吻你 2020-12-02 15:33

I have revealing module pattern which looks like this:

\'use strict\';

angular.module(\'app\', [])
   .directive(\'myDirective\', [\'SomeDep\', function (So         


        
相关标签:
3条回答
  • 2020-12-02 15:53

    You shouldn't need a $watch. Just bind to resize event on window:

    DEMO

    'use strict';
    
    var app = angular.module('plunker', []);
    
    app.directive('myDirective', ['$window', function ($window) {
    
         return {
            link: link,
            restrict: 'E',
            template: '<div>window size: {{width}}px</div>'
         };
    
         function link(scope, element, attrs){
    
           scope.width = $window.innerWidth;
    
           angular.element($window).bind('resize', function(){
    
             scope.width = $window.innerWidth;
    
             // manuall $digest required as resize event
             // is outside of angular
             scope.$digest();
           });
    
         }
    
     }]);
    
    0 讨论(0)
  • 2020-12-02 15:53

    // Following is angular 2.0 directive for window re size that adjust scroll bar for give element as per your tag

    ---- angular 2.0 window resize directive.
    import { Directive, ElementRef} from 'angular2/core';
    
    @Directive({
           selector: '[resize]',
           host: { '(window:resize)': 'onResize()' } // Window resize listener
    })
    
    export class AutoResize {
    
    element: ElementRef; // Element that associated to attribute.
    $window: any;
           constructor(_element: ElementRef) {
    
             this.element = _element;
             // Get instance of DOM window.
             this.$window = angular.element(window);
    
             this.onResize();
    
        }
    
        // Adjust height of element.
        onResize() {
             $(this.element.nativeElement).css('height', (this.$window.height() - 163) + 'px');
       }
    }
    
    0 讨论(0)
  • 2020-12-02 16:03

    You can listen resize event and fire where some dimension change

    directive

    (function() {
    'use strict';
    
        angular
        .module('myApp.directives')
        .directive('resize', ['$window', function ($window) {
            return {
                link: link,
                restrict: 'A'
            };
    
            function link(scope, element, attrs){
                scope.width = $window.innerWidth;
                function onResize(){
                    // uncomment for only fire when $window.innerWidth change   
                    // if (scope.width !== $window.innerWidth)
                    {
                        scope.width = $window.innerWidth;
                        scope.$digest();
                    }
                };
    
                function cleanUp() {
                    angular.element($window).off('resize', onResize);
                }
    
                angular.element($window).on('resize', onResize);
                scope.$on('$destroy', cleanUp);
            }
        }]);
    })();
    

    In html

    <div class="row" resize> ,
        <div class="col-sm-2 col-xs-6" ng-repeat="v in tag.vod"> 
            <h4 ng-bind="::v.known_as"></h4>
        </div> 
    </div> 
    

    Controller :

    $scope.$watch('width', function(old, newv){
         console.log(old, newv);
     })
    
    0 讨论(0)
提交回复
热议问题