属性是应用于所有项目而不仅仅是当前项目?

时间:2015-12-22 06:52:37

标签: javascript angularjs overwrite

我正在使用AngularJS并尝试记录各种项目的独特色调。我有一系列项目和一系列颜色:

<input type='number' ng-model='colors[0].intensity'> //repeat for each color RGB
<button ng-click='apply()'>APPLY</button>

$scope.apply = function(){
  $scope.color_this = angular.copy($scope.colors, $scope.color_this);
  $scope.item[0].colors = angular.copy($scope.color_this, $scope.item[0].colors);
  $scope.color_this = []; // blank array to make room for the next RGB mix
};

我有一个输入,允许我改变每种颜色的强度。当我单击APPLY按钮时,colors []被复制到items array [0] .colors中。

$scope.item[0].colors === $scope.item[1].colors // evaluates to true

这很有用,直到我转到下一个$ scope.item [1] .colors。当我尝试设置$ scope.item [1]的下一个颜色时,前面的项目颜色也会被覆盖。那就是:

Could not resolve all dependencies for configuration ':PagerSlidingTabStripLib:_debugCompile'.
   > Could not resolve com.android.support:support-v4:23.1.0.
     Required by:
         adt-android:PagerSlidingTabStripLib:unspecified
      > Could not resolve com.android.support:support-v4:23.1.0.
         > Could not get resource 'https://jitpack.io/com/android/support/support-v4/23.1.0/support-v4-23.1.0.pom'.
            > Could not GET 'https://jitpack.io/com/android/support/support-v4/23.1.0/support-v4-23.1.0.pom'.
               > Connection to https://jitpack.io refused
      > Could not resolve com.android.support:support-v4:23.1.0.
         > Could not get resource 'https://jcenter.bintray.com/com/android/support/support-v4/23.1.0/support-v4-23.1.0.pom'.
            > Could not GET 'https://jcenter.bintray.com/com/android/support/support-v4/23.1.0/support-v4-23.1.0.pom'.

但我不想要这个!我希望每件商品都能保持自己独特的颜色。我的逻辑是否存在根本性的错误?为什么之前的项目颜色会被覆盖?

1 个答案:

答案 0 :(得分:1)

对我而言,它看起来与Javascript数组的深层复制问题有某种关系。 我建议跳过angular.copy()的第二个参数。 查看文档https://docs.angularjs.org/api/ng/function/angular.copy

像这样......

$scope.apply = function(){
  $scope.color_this = angular.copy($scope.colors);
  $scope.item[0].colors = angular.copy($scope.color_this);
  $scope.color_this = []; // This line is not needed since copy will create a new array each time, so you can remove this
};

试一试。