2

I'm learning things a little backwards because I hopped from jquery to angualrJs without truly learning javascript. I do know some javascript but I'm also very lost on how to implement it with my Angular Projects. My current dilemma is that I'm trying to use javascript in a controller to scroll left or right when a button is hovered over in my view.

My javascript function in my controller is as follows.

(function() {
  'use strict';

  angular
    .module('bhamDesign')
    .controller('GalleryController', GalleryController);


  /** @ngInject */
  function GalleryController($scope, $stateParams, $http) {

    var vm = $scope;

      $http.get('assets/json/galleries/' + $stateParams.mainId + '.json').success(function(data) {
        vm.gallery = data;
      });

      vm.toggle = false;

      vm.imageLightbox = function (image) {
          vm.toggle=!vm.toggle;
          vm.imageSingle = image;
      };

      vm.scrollLeft = function(){
        getElementsByClassName('img-container').scrollLeft += 20;
      };

      vm.scrollRight = function(){
        getElementsByClassName('img-container').scrollRight += 20;
      };

  }

})();

and it is called from the view like so.

<i id="scroll-left" ng-mouseover="scrollLeft()" class="fa fa-angle-left"></i>
<i id="scroll-right" ng-mouseover="scrollRight()" class="fa fa-angle-right"></i>

<div class="img-container"> 
  <img ng-src="{{image}}" ng-repeat="image in gallery.images" ng-click="imageLightbox(image)">
</div>

I feel like I have to call the element from the DOM into my javascript function in order for it to work correctly but I'm a little lost about what I'm actually doing. Can someone help me connect the dots?

3
  • you are missing a reference to your scope object can you post the full file? it would be found off the element on scope. Commented Mar 19, 2016 at 19:13
  • also this isn;t the way I would set up the secondary function. Commented Mar 19, 2016 at 19:14
  • I'm sorry I'm not sure what you mean by scope object. I attached my controller to my route with UI router if that is what you're asking. Also, may I ask how you would set up a secondary function? I read that you shouldn't clutter your controller js with javascript I just don't know a better practice. Commented Mar 19, 2016 at 19:20

1 Answer 1

2

I didn't test this, but this direction can be better for you. I converted your controller to a directive. You might need to create a html template as well. But his might work too.

(function() {
    'use strict';

    function galleryDirective($stateParams, $http, $document) {
        /** @ngInject */
        function controller($scope) {
        }

        function link($scope) {
            var $$document = angular.element($document);

            $http.get('assets/json/galleries/' + $stateParams.mainId + '.json').success(function(data) {
                $scope.gallery = data;
            });

            $scope.toggle = false;

            $scope.imageLightbox = function (image) {
                $scope.toggle=!$scope.toggle;
                $scope.imageSingle = image;
            };

            vm.scrollLeft = function(){
                $$document.getElementsByClassName('img-container').scrollLeft += 20;
            };

            vm.scrollRight = function(){
                $$document.getElementsByClassName('img-container').scrollRight += 20;
            };
        }

        return {
            controller: controller,
            link: link,
            scope: {

            }
        }
    }

    angular
            .module('bhamDesign')
            .directive('galleryDirective', ['$stateParams', '$http', '$document', galleryDirective]);

})();

<gallery-directive>

    <i id="scroll-left" ng-mouseover="scrollLeft()" class="fa fa-angle-left"></i>
    <i id="scroll-right" ng-mouseover="scrollRight()" class="fa fa-angle-right"></i>

    <div class="img-container">
        <img ng-src="{{image}}" ng-repeat="image in gallery.images" ng-click="imageLightbox(image)">
    </div>

</gallery-directive>
Sign up to request clarification or add additional context in comments.

5 Comments

That seems to make sense. Unfortunately, it isn't working.
I guess the document.getElementByClass isn't being registered as a function.
is this function in a directive?
the first problem is that when controller is created document is not yet rendered. have you considering doing this in a resolve? medium.com/opinionated-angularjs/…
Interesting, I've never heard of resolves. I'm going to look into it now.

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.