0

我在 $scope 内有一个函数,它更新 $scope 内的变量 - 我的问题是,一旦函数结束,变量就会被“恢复”到其原始值:

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

}

标记:

<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 在函数内部按预期更新(+-7 天),但 {{firstdayofweek}} 每次调用该函数时都会显示“现在”的更新日期。(控制台显示正确的更新日期)

我猜控制器每次都会刷新 - 有什么办法可以阻止它发生?

谢谢!

4

0 回答 0