2016-09-29 56 views
0

每次用戶更改窗口高度時,我都需要計算元素的位置。所以我創建了一個指令。 ?如何在用戶更改窗口高度時觸發Angular中的事件?

'use strict'; 
    angular.module "myApp" 
    .directive 'uibPosition',['$position','$document','$timeout', ($position,$document,$timeout)-> 
    return { 
     restrict: 'EAC', 
     link: (scope, element, attr) -> 
//I want to Trigger this event when user change windows height 
     $timeout -> 
      if $document.height() - $position.position(element).top < 500 
      element[0].querySelector('.custom-popup-wrapper').style.top = 'auto' 
      element[0].querySelector('.custom-popup-wrapper').style.bottom = 40 + 'px' 
      else 
      element[0].querySelector('.custom-popup-wrapper').style.top = $position.position(element).top+40+'px' 
    } 
    ] 

如何做到這一點我真的很感激

回答

1

可以觀察者添加到指令:

 var w = angular.element($window); 

     scope.getWindowDimensions = function() { 
      return { 
       "h": w.height(), 
       "w": w.width() 
      }; 
     }; 
     var watcherTimeout; 
     scope.$watch(scope.getWindowDimensions, function (newValue) { 
      $timeout.cancel(watcherTimeout); 
      watcherTimeout = $timeout(function(){ 
       scope.callback(); // call callback function 
      }, 300); 
     }, true); 

     w.bind("resize", function() { 
      scope.$apply(); 
     }); 
+0

什麼是'WindowSizeService'? –

+0

這是一個不必要的部分。對不起:) – olysachok

+0

似乎works.thanks –

相關問題