Hadi
Hadi

Reputation: 17289

change array Items dynamically in angularjs

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);
};

})

Upvotes: 1

Views: 146

Answers (2)

jantimon
jantimon

Reputation: 38140

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

Upvotes: 0

jlowcs
jlowcs

Reputation: 1933

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();   
        }
    }
});

Upvotes: 1

Related Questions