Tuesday, 15 September 2015

Angular JS - $scope array variable is not getting updated

I had the following code in my js file which was invoked every time I clicked on a button:

var now = new Date();
var startDate = new Date(new Date() - 1000*3600*24*($scope.maxRange - range));
$scope.timeRange = [startDate.getTime(), now.getTime()];

But the latest value of timeRange was not getting reflected on the UI. Then I found out, that it is creating a new array each time. And this changes the scope of the variable and does not reflect on the page. Solution is to have the same variable reference and update only the values.

$scope.timeRange[0] = startDate.getTime()
$scope.timeRange[1] = now.getTime();

OR use $parent variable

$scope.$parent.timeRange = [startDate.getTime(), now.getTime()];

No comments:

Post a comment