无法使用angularjs重置$ scope变量

时间:2015-03-20 10:02:14

标签: javascript angularjs yeoman

我有一个工厂注入我的所有控制器以初始化和存储状态。

state.js

angular.module('c2gyoApp')
  .factory('state', function() {

    var now = new moment().startOf('hour');

    return {
      rental: {
        tab: 'simple',
        startDate: now.clone().add(1, 'h'),
        endDate: now.clone().add(10, 'h'),
        distance: 10,
        timeMinutes: 0,
        timeHours: 10,
        timeDays: 0,
        timeWeeks: 0,
        timeStanding: 0,
        airport: false
      }
    };
  });

每个控制器的开头都是这样的

c2gdtp.js

angular.module('c2gyoApp')
  .controller('C2gdtpCtrl', [
    '$scope',
    'c2gConfig',
    'duration',
    'state',
    function($scope, config, duration, state) {
      $scope.rental = state.rental;
      ...
    }
  ]);

这在整个不同的控制器中同样适用。对$state.rental对象所做的每个更改都通过不同的视图/控制器保存。

我在每个控制器使用的指令中实现了clear()函数:

timeinputform.js

angular.module('c2gyoApp')
  .directive('timeInputForm', function() {
    return {
      restrict: 'E',
      templateUrl: 'scripts/directives/timeInputForm.html',
      controller: function($scope) {
        ...
        $scope.clear = function() {
          var now = new moment().startOf('hour').add(1, 'h');

          $scope.rental = {
            startDate: now.clone(),
            endDate: now.clone(),
            distance: 0,
            timeMinutes: 0,
            timeHours: 0,
            timeDays: 0,
            timeWeeks: 0,
            timeStanding: 0,
            airport: false
          };

          angular.copy($scope.rental);
        };

      }
    };
  });

问题是它没有保存$state.rental对象的重置。例如,如果我在view1 / controller1中并单击清除按钮,则会重置$state.rental对象。如果我改变到view2 / controller2,我有与之前相同的旧值,我点击了清除按钮。如果我再次访问view1 / controller1,我也有之前相同的旧值,虽然我点击了清除按钮。

clear()函数在指令中,因为清除按钮所在的位置。我尝试将clear函数复制到控制器中,结果相同。

我只想清除所有控制器的状态。这个诀窍是什么?

编辑 @zeroflagL干净的解决方案怎么样?我已经尝试在工厂设置clearRental功能:

编辑#2 我的最终解决方案。标签值必须保持不变,我无法在工厂中将其从$scope中取出。现在它被传递了。

state.js

angular.module('c2gyoApp')
  .factory('state', function() {

    var now = new moment().startOf('hour');

    var rental = {
      tab: 'simple',
      startDate: now.clone().add(1, 'h'),
      endDate: now.clone().add(10, 'h'),
      distance: 10,
      timeMinutes: 0,
      timeHours: 10,
      timeDays: 0,
      timeWeeks: 0,
      timeStanding: 0,
      airport: false
    };

    var clearRental = function(currenttab) {
      var now = new moment().startOf('hour').add(1, 'h');

      var rental = {
        tab: currenttab,
        startDate: now.clone(),
        endDate: now.clone(),
        distance: 0,
        timeMinutes: 0,
        timeHours: 0,
        timeDays: 0,
        timeWeeks: 0,
        timeStanding: 0,
        airport: false
      };

      angular.copy(rental, this.rental);
    };

    return {
      rental: rental,
      clearRental: clearRental
    };
  });

控制器:

c2gdtp.js

angular.module('c2gyoApp')
  .controller('SmdtpCtrl', [
    '$scope',
    'stadtmobilRates',
    'smConfig',
    'duration',
    'state',
    function($scope, stadtmobilRates, smConfig, duration, state) {
      $scope.rental = state.rental;
      $scope.clear = function() {
        state.clearRental($scope.rental.tab);
      };
      ...
    }
  ]);

2 个答案:

答案 0 :(得分:2)

$scope.rental = {
        tab: $scope.rental.tab,

这会将新对象分配给与$scope.rental无关联的state.rental

angular.copy($scope.rental)

基本上没什么。您需要分配更改:

angular.copy($scope.rental, state.rental);

当然,更清洁的解决方案是使用由控制器调用的state.clearRental方法。

修改

至于更清洁的解决方案:

clearRental: function() {
    var now = new moment().startOf('hour').add(1, 'h');

    var rental = { ...};
    angular.copy(rental, this.rental);

$scope.clear = function() {
    state.clearRental();
};

答案 1 :(得分:1)

你可以在指令中调用state:

angular.module('c2gyoApp')
  .directive('timeInputForm', ['state', function(state) {
    return {
      restrict: 'E',
      templateUrl: 'scripts/directives/timeInputForm.html',
      controller: function($scope) {
        ...
        $scope.clear = function() {
          var now = new moment().startOf('hour').add(1, 'h');

          state.rental = $scope.rental = {
            tab: $scope.rental.tab,
            startDate: now.clone(),
            endDate: now.clone(),
            distance: 0,
            timeMinutes: 0,
            timeHours: 0,
            timeDays: 0,
            timeWeeks: 0,
            timeStanding: 0,
            airport: false
          };

          angular.copy($scope.rental);
        };

      }
    };
  }]);