如果屏幕宽度小于800像素,Angularjs会做一些事情

时间:2015-09-24 13:08:15

标签: angularjs

如果屏幕宽度小于800像素,如何让Angularjs做某事?我类似的jquery代码

if (screen.width < 800) {
alert('Less than 800');
}
else {

alert('More than 800');
}

1 个答案:

答案 0 :(得分:0)

这是一个监视窗口大小的指令示例。不久:使用$ watch和$ window.width()

app.directive('resize', function ($window) {
return function (scope, element) {
    var w = angular.element($window);
    scope.getWindowDimensions = function () {
        return {
            'h': w.height(),
            'w': w.width()
        };
    };
    scope.$watch(scope.getWindowDimensions, function (newValue, oldValue) {
        scope.windowHeight = newValue.h;
        scope.windowWidth = newValue.w;

        scope.style = function () {
            return {
                'height': (newValue.h - 100) + 'px',
                    'width': (newValue.w - 100) + 'px'
            };
        };

    }, true);

    w.bind('resize', function () {
        scope.$apply();
    });
  }
})
相关问题