angular js 滚动指令不工作
angular js scrolling directive not working
我正在尝试执行 angular (1.3.14) 指令来处理这样的元素上的滚动事件
var app = angular.module('myApp', []);
app.directive("scroll", function ($window) {
return {
restrict: 'A',
link: function(scope, element, attrs) {
console.log(element.className); // return 'undefined'
element.on('scroll', function(e) {
console.log('scroll'); //not working
});
element.on('click', function(e) {
console.log('click'); //working
});
}
}
});
我的问题是滚动事件没有触发。单击等所有其他事件都可以正常工作,但不能滚动。此外,当我尝试获取元素的 class 时,我得到 'undefined' 并且我的元素具有 class。这是 html:
<body ng-app="myApp" ng-controller="myCtrl" ng-keydown="keyListener($event)">
<section class="dark content second" scroll="">
</section>
</body>
我不知道这里有什么问题。
您的指令是正确的,我已经在您的部分中使用内部 div 进行了测试,并添加了一些 类 使其可滚动
<section class="dark content second" scroll="">
Hi
<div class="internal">
Something
</div>
</section>
CSS
.second{
background-color: red;
max-height: 150px;
overflow-y:scroll;
}
.internal{
height: 200px;
}
活动进行得非常顺利!您只需要使 <section>
可滚动或在 body/html 标记中应用该指令。这是我测试过的 Plunker 示例 http://plnkr.co/edit/hp2BbnLeGjtwIbfi2mqZ?p=preview
试试这个
console.log(attrs.class);
element.bind('scroll', function() {
console.log('scroll');
});
我正在尝试执行 angular (1.3.14) 指令来处理这样的元素上的滚动事件
var app = angular.module('myApp', []);
app.directive("scroll", function ($window) {
return {
restrict: 'A',
link: function(scope, element, attrs) {
console.log(element.className); // return 'undefined'
element.on('scroll', function(e) {
console.log('scroll'); //not working
});
element.on('click', function(e) {
console.log('click'); //working
});
}
}
});
我的问题是滚动事件没有触发。单击等所有其他事件都可以正常工作,但不能滚动。此外,当我尝试获取元素的 class 时,我得到 'undefined' 并且我的元素具有 class。这是 html:
<body ng-app="myApp" ng-controller="myCtrl" ng-keydown="keyListener($event)">
<section class="dark content second" scroll="">
</section>
</body>
我不知道这里有什么问题。
您的指令是正确的,我已经在您的部分中使用内部 div 进行了测试,并添加了一些 类 使其可滚动
<section class="dark content second" scroll="">
Hi
<div class="internal">
Something
</div>
</section>
CSS
.second{
background-color: red;
max-height: 150px;
overflow-y:scroll;
}
.internal{
height: 200px;
}
活动进行得非常顺利!您只需要使 <section>
可滚动或在 body/html 标记中应用该指令。这是我测试过的 Plunker 示例 http://plnkr.co/edit/hp2BbnLeGjtwIbfi2mqZ?p=preview
试试这个
console.log(attrs.class);
element.bind('scroll', function() {
console.log('scroll');
});