【问题标题】:My directive in angular not working我的角度指令不起作用
【发布时间】:2015-09-17 07:31:30
【问题描述】:

我对 Angular 有点陌生。

我想在文档宽度发生变化时有一个监听器。

所以首先我决定像这样添加一个 $watch:

$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-view 添加指令:

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

但是控制台没有任何效果,也没有错误,我不知道该怎么办!

有什么帮助吗?

【问题讨论】:

标签: javascript angularjs


【解决方案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>

现在元素的大小基本上被调整为窗口高度 - 您可以指定的高度(在本例中为 270 像素)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-25
    相关资源
    最近更新 更多