I'm very new to AngularJS and I try to do the following:
Bind an scroll event on an element by using a custom directive. Here's the code:
First of all, my controller:
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.');
}
}])
You'll notice, that here I have function called 'CustomAlert'. I don't know why I'm binding it to $scope, I've only found this kind of information on the next. Can I remove the scope or can someone explain my why it's important?
Then I have the directive:
.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.');
});
}
}
});
The console.log is executed, so that's not a problem, it's executed, but on the scope.customAlert() I receive the following error:
scope.customAlert is not a function.
I've found to do it like follow:
scope.$apply('customAlert()');
However, then I receive $apply is already in progress.
Anyone has an idea how I should achieve this?
Kind regards,