1

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,

7
  • I do call it as: <ul ngc-scroll ... But that's working since the on scroll event is executed, but I don't get it to recognize my customAlert function :s Commented Nov 29, 2014 at 20:26
  • does scope.customAlert(); exists in the controller? Commented Nov 29, 2014 at 21:15
  • It does, but it seems that I've found the solution. However, I didn't know what I changed :) Commented Nov 29, 2014 at 21:16
  • Could you post your solution ? Commented Nov 29, 2014 at 21:17
  • I'll update it as soon as the code is finished because it's very cluttery right now. Commented Nov 29, 2014 at 21:17

4 Answers 4

1

Try this,

1

myApp.directive("ngcScroll", function($rootScope) {
    return {
        restrict: 'A',
        link: function (scope, element, attributes, ctrl) {  
            element.on('DOMMouseScroll mousewheel', function (e) {
                scope.$apply(function () {
                    scope.customAlert();
                })
            });
        }
    }
});

Demo: http://jsfiddle.net/HB7LU/8642/

2

<div ng-controller="MyCtrl">   
    <div ngc-scroll custom-alert="customAlert()"> ConTEN </div>   
</div>

JS

myApp.directive("ngcScroll", function() {
    return {
        restrict: 'A',
        scope: {
            customAlertFn: "&customAlert"
        },
        link: function (scope, element, attributes) {           
            element.on('DOMMouseScroll mousewheel', function (e) {
                scope.customAlertFn();
            });
        }
    }

});

& operator allows you to invoke or evaluate an expression on the parent scope of whatever the directive

Demo: http://jsfiddle.net/HB7LU/8640/

3

<div ngc-scroll> ConTEN </div> 

JS

var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl', function ($scope, $rootScope) {
    $scope.customAlert = function() {
        console.log('This ia scrolling demo.');
    }
    
    $scope.$on('customAlert', $scope.customAlert);
});

myApp.directive("ngcScroll", function($rootScope) {
    return {
        restrict: 'A',
        link: function (scope, element, attributes) {  
            element.on('DOMMouseScroll mousewheel', function (e) {
                scope.$emit('customAlert');
            });
        }
    }
});
Sign up to request clarification or add additional context in comments.

Comments

0

Ok,

I've found the solution, which was quite simple in fact. I had defined the module in a wrong controller, but right now it's working without an issue.

This means the code looks like:

Controller:

.controller('OfficeUI', ['$scope', '$http', function($scope, $http) {
    var application = this;

    // Load up the JSon data string containing the definition of the ribbon.
    $http.get('/OfficeUI/Resources/JSon/application.json').
        success(function (data) {
            application.title = data.Title;
            application.icons = data.Icons;
        }).
        error(function (data) {
            // An error has occured while retrieving the data, so write it away in the logs.
            console.log('An error occured while retrieving the data.');
        });
}])

Directive:

.directive("ngcScroll", function() {
    return {
        restrict: 'A',
        link: function (scope, element, attributes) {
            element.on('DOMMouseScroll mousewheel', function (e) {
                scope.$apply(attributes['ngcScroll'], 'demo');
            });
        }
    }
});

HTML:

<div ngc-scroll="enableTab(this)" class="ribbon officeui-borders-border-bottom-grey" ng-controller="OfficeUIRibbon as ribbon">
    <ul role="tabs" class="officeui-space-no-margin officeui-space-no-padding officeui-borders-border-bottom-grey">
        <li role="tab" ng-repeat="tab in ribbon.tabs" ng-class="{ application: $first, active: !$first && ribbon.isActive(tab.Id) }" class="officeui-display-inline-block" ng-click="$first || ribbon.setTab(tab.Id)">
            <span>{{tab.Name}}</span>
            <div id="{{tab.contentId}}" ng-class="{ 'officeui-display-block': !first && ribbon.isActive(tab.Id), 'officeui-display-hidden': !first && !ribbon.isActive(tab.Id)}" class="contents officeui-position-absolute">
                {{tab.Contents}}
            </div>
        </li>
    </ul>
</div>

4 Comments

Good work. I'm glad you posted the solution to this. I've learned something new. I can't upvote as i have too many votes today.
What about "I would just pass a function from the main controller without the = sign." ?
@Alexander I'm sorry, I was wrong. I've removed that question.
@Complexity this condition changes everything... :). Anyway thanks that shared your solution...
0

From inside your directive code this should work:

link: function (scope, element, attributes) {
    scope.$parent.customAlert();
    element.on('DOMMouseScroll mousewheel', function (e) {
        console.log('Element is being executed.');
    });
}

Access any methods and properties on the parent scope via:

scope.$parent

The caveat here is while this is a pretty direct way to access a parent scope's method, it's pretty inflexible as it will always be making assumptions about what exists on the parent scope. Using an isolate scope as suggested by Alexander gives much greater flexibility. When you place your directive inside different parent scopes you can pass the actual parent method you want to call into the directive via the HTML attributes.

Comments

0

var myApp = angular.module('myApp',['ngRoute']);

//myApp.directive('myDirective', function() {});
//myApp.factory('myService', function() {});


myApp.controller("MyCtrl",['$scope',function($scope){
    $scope.$on("receive",function(f,e){
        alert(e);
    });
}]);
myApp.directive("ngcScroll", function() {
    return {
        restrict: 'A',
        link: function (scope, element, attributes) {
            element.on('DOMMouseScroll mousewheel', function (e) {
               scope.$emit("receive","djfhdfdhfdhfgdhf");
            });
        }
}

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://code.angularjs.org/1.2.27/angular-route.js"></script>

<div ng-app="myApp">
<div ng-controller="MyCtrl">
    <div ngc-scroll>
        A root scope can be retrieved using the $rootScope key from the $injector. Child scopes are created using the $new() method. (Most scopes are created ...<br /><br /><br /><br /><br />  A root scope can be retrieved using the $rootScope key from the $injector. Child scopes are created using the $new() method. (Most scopes are created ...<br /><br /><br /><br /><br />  A root scope can be retrieved using the $rootScope key from the $injector. Child scopes are created using the $new() method. (Most scopes are created ...<br /><br /><br /><br /><br />  A root scope can be retrieved using the $rootScope key from the $injector. Child scopes are created using the $new() method. (Most scopes are created ...<br /><br /><br /><br /><br />  A root scope can be retrieved using the $rootScope key from the $injector. Child scopes are created using the $new() method. (Most scopes are created ...<br /><br /><br /><br /><br />  A root scope can be retrieved using the $rootScope key from the $injector. Child scopes are created using the $new() method. (Most scopes are created ...<br /><br /><br /><br /><br />  A root scope can be retrieved using the $rootScope key from the $injector. Child scopes are created using the $new() method. (Most scopes are created ...<br /><br /><br /><br /><br />  A root scope can be retrieved using the $rootScope key from the $injector. Child scopes are created using the $new() method. (Most scopes are created ...<br /><br /><br /><br /><br />  A root scope can be retrieved using the $rootScope key from the $injector. Child scopes are created using the $new() method. (Most scopes are created ...<br /><br /><br /><br /><br />  A root scope can be retrieved using the $rootScope key from the $injector. Child scopes are created using the $new() method. (Most scopes are created ...<br /><br /><br /><br /><br />  A root scope can be retrieved using the $rootScope key from the $injector. Child scopes are created using the $new() method. (Most scopes are created ...<br /><br /><br /><br /><br />  A root scope can be retrieved using the $rootScope key from the $injector. Child scopes are created using the $new() method. (Most scopes are created ...<br /><br /><br /><br /><br />  A root scope can be retrieved using the $rootScope key from the $injector. Child scopes are created using the $new() method. (Most scopes are created ...<br /><br /><br /><br /><br />  A root scope can be retrieved using the $rootScope key from the $injector. Child scopes are created using the $new() method. (Most scopes are created ...<br /><br /><br /><br /><br />  A root scope can be retrieved using the $rootScope key from the $injector. Child scopes are created using the $new() method. (Most scopes are created ...<br /><br /><br /><br /><br />  A root scope can be retrieved using the $rootScope key from the $injector. Child scopes are created using the $new() method. (Most scopes are created ...<br /><br /><br /><br /><br />  A root scope can be retrieved using the $rootScope key from the $injector. Child scopes are created using the $new() method. (Most scopes are created ...<br /><br /><br /><br /><br />  A root scope can be retrieved using the $rootScope key from the $injector. Child scopes are created using the $new() method. (Most scopes are created ...<br /><br /><br /><br /><br />  A root scope can be retrieved using the $rootScope key from the $injector. Child scopes are created using the $new() method. (Most scopes are created ...<br /><br /><br /><br /><br />  A root scope can be retrieved using the $rootScope key from the $injector. Child scopes are created using the $new() method. (Most scopes are created ...<br /><br /><br /><br /><br />  A root scope can be retrieved using the $rootScope key from the $injector. Child scopes are created using the $new() method. (Most scopes are created ...<br /><br /><br /><br /><br />  A root scope can be retrieved using the $rootScope key from the $injector. Child scopes are created using the $new() method. (Most scopes are created ...<br /><br /><br /><br /><br />  A root scope can be retrieved using the $rootScope key from the $injector. Child scopes are created using the $new() method. (Most scopes are created ...<br /><br /><br /><br /><br />  A root scope can be retrieved using the $rootScope key from the $injector. Child scopes are created using the $new() method. (Most scopes are created ...<br /><br /><br /><br /><br />


    </div>

</div>
  
  </div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.