简体   繁体   English

我的角度指令无效

[英]My directive in angular not working

I'm kinda new to angular. 我是新手。

I want to have a listener for whenever width of document changes. 每当文档宽度更改时,我都希望有一个侦听器。

So first of all I decided to add a $watch like this: 所以首先我决定添加一个$ watch像这样:

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

But it only works on page load and not on resize. 但是它仅适用于页面加载,而不适用于调整大小。

Then I decided to write a Directive like this: 然后,我决定编写这样的指令:

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);
                });
            }
        };
    }]);

And add directive to my ng-view: 并将指令添加到我的ng-view中:

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

But nothing work and no error in console and I don't know what should I do! 但是没有任何效果,控制台也没有错误,我也不知道该怎么办!

Any help please? 有什么帮助吗?

Hi this is my approach to resize the a specific element 嗨,这是我调整特定元素大小的方法

.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'
                };
        }
    }
})

In your html it would look like this 在您的html中,它看起来像这样

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

Basically the element is now resizes down to window-height - the height u can specify ( in this case 270px) 现在基本上可以将元素的大小调整为window-height-您可以指定的高度(在这种情况下为270px)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM