I have a function inside a $scope which updates a variable inside the $scope - my problem is that the variable is being "restored" to its original value once the function ends:
function MyCtrl($scope, $http, $templateCache) {
$scope.changeWeek = function (direction) {
console.log(direction);
console.log($scope.firstdayofweek);
var d = new Date();
direction == '7' ? d.setDate($scope.firstdayofweek.getDate() + 7) : d.setDate($scope.firstdayofweek.getDate() - 7);
$scope.firstdayofweek = d;
console.log(d);
console.log($scope.firstdayofweek);
}
$scope.firstdayofweek = getMonday(new Date());
$http({ method: 'GET', url: '/TimeSheet/Services/GetSubEmployees.ashx', cache: $templateCache }).
success(function (data, status) {
$scope.status = status;
$scope.fullname = data.fullname;
$scope.accountname = data.accountname;
$scope.domain = data.domain;
$scope.subemployees = data.subemployees;
$scope.domain == 'IL' ? $scope.firstdayofweek.setDate($scope.firstdayofweek.getDate() - 1) : $scope.firstdayofweek.setDate($scope.firstdayofweek.getDate());
}).
error(function (data, status) {
$scope.data = data || "Request failed";
$scope.status = status;
});
}
The markup:
<div class="row" >
<div class="span1"><a href="#" ng-click="changeWeek(-7)"></a></div>
<div ng-repeat="n in [] | range:7" class="span1">
{{(firstdayofweek.getDate()+$index)+'/'+(firstdayofweek.getMonth()+1)}}
</div>
<div class="span1"><a href="#" ng-click="changeWeek(7)"></a></div>
</div>
$scope.firstdayofweek is updated as expected inside the function (+-7 days) but {{firstdayofweek}} shows the updated date of "now" everytimg I call the function. (The console shows the right updated date)
I'm guessing the controller is refreshed every time - any way I can stop it from happening?
Thanks!