2015-09-17 129 views
0

我有点新的角度。我的角度不工作的指令

我希望每当文档宽度变化时都有一个侦听器。

所以首先我决定增加一个$表是这样的:

$scope.$watch(function(){ 
    return $window.innetWidth; 
}, function(n, o){ 
    console.log(n); 
    console.log(o); 
}); 

但它只能在页面加载,而不是调整。

于是我决定写这样的指令:

app.directive('watchWidth', [function(){ 
     return { 
      restrict: 'A', 
      link: function(scope, element) { 
       element.style.display = 'none'; 

       $scope.$watch(function() { 
        return element.innerWidth; 
       }, function (n,o) { 
        console.log(n); 
        console.log(o); 
       }); 
      } 
     }; 
    }]); 

并添加指令到我的NG-观点:

<div watchWidth ng-view class="main-wrapper"></div> 

但没有工作,在控制台没有错误,我不知道我该怎么做!

请帮忙吗?

+0

你是否看到这篇文章http://stackoverflow.com/questions/23044338/window-resize-directive? – aSoler

+0

试试这个:'clientWidth'而不是'innerWith' –

+0

或者用jquery:'$(element).innerWidth()' –

回答

1

你好,这是我的方法来调整特定元素

.directive('resize', function (Ls) { 
    return function (scope, element, attr) { 
     scope.resizeWithOffset = function (offsetH) { 
       return { 
        'min-height': (window.innerHeight - offsetH) + 'px', 
        'max-height': (window.innerHeight - offsetH) + 'px' 
       }; 
     } 
    } 
}) 

在HTML它看起来像这样

<ion-scroll ng-style="resizeWithOffset(270)" resize></ion-scroll> 

基本元素,现在重新调整到窗口高度 - 身高你可以指定(在这种情况下为270px)