1

我想要实现的目标:
在更改更新 $storage 数组时观看“身高”、“胸部”、“腰部”、“臀部”和“大腿”范围。

当前问题:
尽管下面的方法有效,但我必须将每个部分重复 5 次,感觉这可以精简和清理!

我不能 100% 确定这是否是最好的方法,一些保证和建议会有所帮助!

谢谢你。

  defaultStats();
  function defaultStats() {
    if ($scope.$storage.userData.bodyType === undefined) {
      $scope.$storage.userData.bodyType = (
        {'height': 525,'chest': 32,'waist': 30,'hips': 34,'thighs': 20,'bodyType': bodyType}
      );
    }
    $scope.height = $scope.$storage.userData.bodyType.height;
    $scope.chest = $scope.$storage.userData.bodyType.chest;
    $scope.waist = $scope.$storage.userData.bodyType.waist;
    $scope.hips = $scope.$storage.userData.bodyType.hips;
    $scope.thighs = $scope.$storage.userData.bodyType.thighs;
  }
  var bodyType = 'triangle';
  $scope.$watch('height', function() {
    $scope.$storage.userData.bodyType = (
      {'height': $scope.height,'chest': $scope.chest,'waist': $scope.waist,'hips': $scope.hips,'thighs': $scope.thighs,'bodyType': bodyType}
    );
  });
  $scope.$watch('chest', function() {
    $scope.$storage.userData.bodyType = (
      {'height': $scope.height,'chest': $scope.chest,'waist': $scope.waist,'hips': $scope.hips,'thighs': $scope.thighs,'bodyType': bodyType}
    );
  });
  $scope.$watch('waist', function() {
    $scope.$storage.userData.bodyType = (
      {'height': $scope.height,'chest': $scope.chest,'waist': $scope.waist,'hips': $scope.hips,'thighs': $scope.thighs,'bodyType': bodyType}
    );
  });
  $scope.$watch('hips', function() {
    $scope.$storage.userData.bodyType = (
      {'height': $scope.height,'chest': $scope.chest,'waist': $scope.waist,'hips': $scope.hips,'thighs': $scope.thighs,'bodyType': bodyType}
    );
  });
  $scope.$watch('thighs', function() {
    $scope.$storage.userData.bodyType = (
      {'height': $scope.height,'chest': $scope.chest,'waist': $scope.waist,'hips': $scope.hips,'thighs': $scope.thighs,'bodyType': bodyType}
    );
  });

更新

  // Default Stats (Average UK Woman)
  defaultStats();
  function defaultStats() {
    if ($scope.$storage.userData.bodyType === undefined) {
      $scope.$storage.userData.bodyType = (
        {'height': 525,'chest': 32,'waist': 30,'hips': 34,'thighs': 20,'bodyType': bodyType}
      );
    }
    $scope.height = $scope.$storage.userData.bodyType.height;
    $scope.chest = $scope.$storage.userData.bodyType.chest;
    $scope.waist = $scope.$storage.userData.bodyType.waist;
    $scope.hips = $scope.$storage.userData.bodyType.hips;
    $scope.thighs = $scope.$storage.userData.bodyType.thighs;
  }
  var bodyType = 'triangle';
  $scope.$watchGroup(['height', 'chest', 'waist', 'hips', 'thighs'], function() {
     $scope.$storage.userData.bodyType = ({'height': $scope.height,'chest': $scope.chest,'waist': $scope.waist,'hips': $scope.hips,'thighs': $scope.thighs,'bodyType': bodyType});
  });
  // Reset Stats
  $scope.resetStats = function() {
    // Reset
    $scope.$storage.userData.bodyType = (
      {'height': 525,'chest': 32,'waist': 30,'hips': 34,'thighs': 20, 'bodyType': ''}
    );
    defaultStats();
  };
4

1 回答 1

3

我建议使用$watchGroup

所以你的观察者应该看起来像

$scope.$watchGroup(['height', 'chest', 'waist', 'hips', 'thighs'], function() {
     $scope.$storage.userData.bodyType = ({'height': $scope.height,'chest': $scope.chest,'waist': $scope.waist,'hips': $scope.hips,'thighs': $scope.thighs,'bodyType': bodyType});
});

文档链接:$watchGroup

于 2015-10-14T10:04:40.973 回答