绑定到 ng-view 内的指令中的事件不起作用 [英] Binding to events in a directive which is inside a ng-view doesn't work
问题描述
我有一个 AngularJS 应用程序,它以前没有使用过内置路由.我正在重构网站,因此它将成为一个 SPA.
I have an AngularJS application, which hasn't been using the built in routing before now. I am refactoring the site, so it will be a SPA.
为此,我将应用程序更改为使用 ng-view 在不同页面之间切换,而不仅仅是让服务器为不同的控制器提供服务
To do this I have change the app to use ng-view to switch between the different pages, instead of just having the server serve the different controllers
完成后,我的无尽卷轴突然停止工作.
After this is done my endless scroll suddenly stopped working.
我有一个如下所示的指令:
I have a directive which looks like this:
directiveModule.directive('whenScrolled', ['$window', function($window) {
return function(scope, element, attr) {
var raw = element[0];
angular.element($window).bind('scroll', function() {
console.log('test');
scope.$apply(attr.whenScrolled);
});
};
}]);
但现在滚动事件永远不会被触发.
But now the scroll event is never fired.
如果我取出 angular.element($window).bind('scroll', function() {..});
并在控制器中使用它而不是它工作正常,但它只是看起来像一个黑客.
If I take angular.element($window).bind('scroll', function() {..});
out and uses it in the controller instead it works fine, but it just seems like a hack.
有什么方法可以绑定到指令内的页面滚动事件,指令位于控制器内,控制器位于 ng-view 内?
Is there any way to bind to the page scroll event inside a directive, which is inside a controller, which is inside a ng-view?
推荐答案
您确定控制器内部的带有指令的元素然后将 ng-view 添加到 DOM 中吗?
Are you sure the element with the directive which is inside the controller and then the ng-view is added to the DOM?
更多代码(A Fiddle)会有所帮助.
More code (A Fiddle) would help.
这篇关于绑定到 ng-view 内的指令中的事件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!