Hello, I am trying to write a module that will contain a service and a directive (the directive will apply to the service for a value), allowing the paralax effect to be applied to various blocks, in my case to images. My logic is, there is a service that, when turned on, will work 1 time and register the window event on scroll and will write it to a variable that needs to be seen globally in the service, and there is a directive that constantly applies the translate property to the element. But I feel that I am doing something very wrong, + I made a mistake somewhere and can’t make the directive see the value of a service. It looks like this:
angular.module("paralaxServices", []).service("mainParalaxService", paralax).directive("eParalaxable", paralaxable); function paralax($window) { var service = { currentWindowPosition: 10 } angular.element($window).on("scroll", setCurrentWindowPosition); return service; }; function setCurrentWindowPosition(event) { currentWindowPosition = this.pageYOffset; } function paralaxable(mainParalaxService) { var directive = { restrict: "A", link: link }; return directive; function link(scope, element, attrs) { element.css("transform", "translate(0%, " + mainParalaxService.currentWindowPosition + "%"); } } On the view it looks like this:
<body ng-controller="mainController"> <!--этот контроллер, как и ng-app объявлены в другом модуле, который использует paralaxServices. первый и 3й див просто для отступов--> <div style="width: 100%; height: 100px; background-color:black"></div> <div e-paralaxable class="container-for-paralax-image"> </div> <div class="container-for-paralax-image2"> </div> Please help me understand what I am doing wrong, and do I use the correct approach?