控制器内部函数不会更新$ scope变量

时间:2013-03-21 12:35:37

标签: angularjs angularjs-scope

我在$ 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}}显示我现在称之为函数的“now”每日更新日期。 (控制台显示正确的更新日期)

我猜每次都会刷新控制器 - 我能以任何方式阻止它发生吗?

谢谢!

0 个答案:

没有答案