【发布时间】:2014-11-29 19:42:52
【问题描述】:
我对 AngularJS 很陌生,我尝试执行以下操作:
使用自定义指令在元素上绑定滚动事件。 代码如下:
首先,我的控制器:
var officeUIApplication = angular.module('OfficeUI.Ribbon.Module', [])
.controller('OfficeUI', ['$scope', '$http', function($scope, $http) {
var application = this;
$scope.customAlert = function() {
console.log('This ia scrolling demo.');
}
}])
您会注意到,这里我有一个名为“CustomAlert”的函数。我不知道我为什么要把它绑定到 $scope,我只在下一个找到了这种信息。我可以删除范围还是有人可以解释我为什么它很重要?
然后我有指令:
.directive("ngcScroll", function() {
return {
restrict: 'A',
link: function (scope, element, attributes) {
scope.customAlert();
element.on('DOMMouseScroll mousewheel', function (e) {
console.log('Element is being executed.');
});
}
}
});
console.log 已执行,所以这不是问题,它已执行,但在 scope.customAlert() 上我收到以下错误:
scope.customAlert is not a function.
我发现这样做如下:
scope.$apply('customAlert()');
但是,我收到 $apply is already in progress.
有人知道我应该如何实现这一目标吗?
亲切的问候,
【问题讨论】:
-
我确实将其称为:
-
执行 scope.customAlert();存在于控制器中?
-
确实如此,但我似乎找到了解决方案。但是,我不知道我改变了什么:)
-
你能发布你的解决方案吗?
-
我会在代码完成后立即更新它,因为它现在非常混乱。
标签: javascript html angularjs