Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJs Directive doesn't work until page reload

I'm trying to build an angularJs directive which will make a div sticky. But I'm stuck in the initial state.

The problem is, this scroll event fires only on page reload. Doesn't work on route change initially. But once I reload the page it work's even with route change.

What am I doing wrong here?

Codes are following:

(function (angular, window) {
    "use strict";

    function stickyDivDirective($compile, $timeout) {
        function directiveLink(scope, element, attributes) {

            function scrollFunction(targetDocument) {
                console.log('Scrolling');
            }

            angular.element(document.querySelector('md-content')).on('scroll', scrollFunction);
        }
        
        var directive = {
            restrict: "A",
            scope: {
                mainContainer: "@",
                targetContainer: "@",
                offsetHeight: '@',
                scrollableElement: '@'
            },
            link: directiveLink
        };
        return directive;
    }

    stickyDivDirective.$inject = ["$compile", "$timeout"];
    angular
        .module(appSuite.module + ".directives")
        .directive("stickyDiv", stickyDivDirective);
})(window.angular, window);

<div class=""
     id="saa__variablesBar"
     sticky-div>
<div>

;

like image 224
Sabbir Ahmed Siddiquee Avatar asked Aug 18 '26 08:08

Sabbir Ahmed Siddiquee


1 Answers

I found the solution. This is really weird. I just used getElementById instead of querySelector. The issue resolved.

like image 112
Sabbir Ahmed Siddiquee Avatar answered Aug 20 '26 00:08

Sabbir Ahmed Siddiquee