1

i want to create timeline in angular. in this timeline years change with mouse wheel event.for mouse wheel event i use hamster.js library. How to change array items by mouse wheel event? here is my code:

angular.module('myApp', [])
.controller("Slider", function ($scope) {
var mouseWheel= document.getElementById('slider');

var hammer1 = Hamster(mouseWheel);
$scope.state = { zoom: 1, top: 0 };
$scope.dragY = 0;
 $scope._calc = function(state) {
    var years = [];
    var top = $scope.state.top;
    for (var i = -4000; i <= 2015; i += state.zoom) {
        var obj = {top: top, year: i};
        years.push(obj);
        top += 40;
    }
    return years;
}
$scope.years = $scope._calc($scope.state);
    hammer1.wheel(function(event, delta){
        if (delta > 0) {
          $scope.zoomOut();  // this function not working
        } else if (delta < 0) {
                $scope.zoomIn();   
        }
    });
$scope.zoomOut= function () {
    $scope.state.zoom += 10;
    $scope.years = $scope._calc($scope.state);
};

})

2 Answers 2

1

My bet is that your Hamster callback does not trigger a digest. Try using $scope.$apply():

hammer1.wheel(function(event, delta){
    $scope.$apply(function () {
        if (delta > 0) {
            $scope.zoomOut();  // this function not working
        } else if (delta < 0) {
            $scope.zoomIn();   
        }
    }
});
Sign up to request clarification or add additional context in comments.

Comments

0

You have to call $scope.$digest(); or $scope.$apply(); so angular knows that it should rerender:

hammer1.wheel(function(event, delta){
        if (delta > 0) {
          $scope.zoomOut();  // this function not working
        } else if (delta < 0) {
          $scope.zoomIn();   
        }
        $scope.$digest();
    });

See also digest and apply explained in detail

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.