角JS - 滚动$窗口 [英] Angular JS - Scroll $window

查看:136
本文介绍了角JS - 滚动$窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我有点难倒。

我想觉得从一个jQuery背景的角度的方式。

I'm trying to think the angular way coming from a jQuery background.

问题:
我只是想隐藏一个固定的元素,如果不滚动的窗口。如果有人向下滚动页面,我想隐藏的元素。

The problem: I'd just like to hide a fixed element if the window is not scrolled. If someone scrolls down the page I would like to hide the element.

我试图创建一个自定义的指令,但我不能得到它的工作作为滚动事件无法。我想一个简单的控制器像下面,但它甚至犯规运行。

I've tried creating a custom directive but I couldnt get it to work as the scroll events were not firing. I'm thinking a simple controller like below, but it doesnt even run.

控制器:

.controller('MyCtrl2', function($scope,appLoading, $location, $anchorScroll, $window ) {
   angular.element($window).bind("scroll", function(e) {
       console.log('scroll')
       console.log(e.pageYOffset)
       $scope.visible = false;
   })
})

查看

<a ng-click="gotoTop()" class="scrollTop" ng-show="{{visible}}">TOP</a>

LIVE preVIEW
<一href=\"http://www.thewinetradition.com.au/new/#/portfolio\">http://www.thewinetradition.com.au/new/#/portfolio

任何意见将大大AP preciated。

Any ideas would be greatly appreciated.

推荐答案

一个基本指示是这样的。其中关键的一点是,你需要调用范围。$适用(),因为滚动将外部运行正常的消化周期。

A basic directive would look like this. One key point is you'll need to call scope.$apply() since scroll will run outside of the normal digest cycle.

app = angular.module('myApp', []);
app.directive("scroll", function ($window) {
    return function(scope, element, attrs) {
        angular.element($window).bind("scroll", function() {
            scope.visible = false;
            scope.$apply();
        });
    };
});

我发现这个的jsfiddle这表明它很好 http://jsfiddle.net/88TzF/

这篇关于角JS - 滚动$窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆