使用splice从数组中删除元素后。它没有重置。我的代码有任何错误

时间:2015-04-16 10:34:52

标签: javascript jquery arrays angularjs

下拉列表包含数组值。如果我从下拉列表中选择一个值,它将从其工作的数组中删除该值。但是点击重置按钮,它应该重置旧值。这是我的代码

HTML代码

<html>
<head>
<script src="angular/angular.js"></script>
<script src="js/controllers.js"></script>
</head>
<body ng-app="myApp">
<div ng-controller="exerciseTypeCtrl">
   <select id="exerciseSuperCategory" data-role="listview" ng-options="Lay.id as Lay.value for Lay in Layer " ng-model="itemsuper" ng-change="changeData(itemsuper)">
   </select> 
   <input type="button" ng-click="resetLayer()" value="Reset"/>        

</div>
</body>
</html>

angularjs控制器代码

<script>
var myApp = angular.module('myApp',[]);

myApp.controller('exerciseTypeCtrl',function($scope)
{

    $scope.Layer = [
        { id: 1, value: '0.38'},
        { id: 2, value: '0.76'},
        { id: 3, value: '1.14'},
        { id: 4, value: '1.52'},
        { id: 5, value: '1.9'},
        { id: 6, value: '2.28'},
        { id: 7, value: '2.66'},
        { id: 8, value: '3.04'},
        { id: 9, value: '3.42'},
        { id: 10, value:'3.8'},
        { id: 11, value: '4.18'},
        { id: 12, value: '4.56'}
    ];

     $scope.changeData = function(value)
      {

         var coating = $scope.Layer;
         if(coating != null)
         {
                var j = coating.length;                                 
                while(j>0)
                {
                    j =j-1;
                    var make = coating[j]['id'];    
                    var present = 0;


                        if(make == value)
                        {                                                           
                          coating.indexOf(make);
                          coating.splice(j,1);                        
                        }

                }
         }
      }
      $scope.resetLayer -function()
      {
          $scope.Layer =  $scope.Layer;
      }

});
</script> 

使用splice我删除了下拉列表选择的值。但点击按钮不重置

提前致谢

3 个答案:

答案 0 :(得分:2)

在初始化/获取Layer数据

时,您应该获取变量的副本
var copyOfLayer = angular.copy($scope.Layer);

然后在重置时,您需要将旧数组分配给$scope.Layer,您还需要将resetLayer函数重写到下面

  $scope.resetLayer = function() {
    $scope.Layer = angular.copy(copyOfLayer)
  }

Working Plunkr

答案 1 :(得分:1)

错误在这里:

$scope.resetLayer -function()

 $scope.resetLayer =function()

我正在格式化你的代码:

<script>
var myApp = angular.module('myApp',[]);

myApp.controller('exerciseTypeCtrl',function($scope)
{

    $scope.Layer = [
        { id: 1, value: '0.38'},
        { id: 2, value: '0.76'},
        { id: 3, value: '1.14'},
        { id: 4, value: '1.52'},
        { id: 5, value: '1.9'},
        { id: 6, value: '2.28'},
        { id: 7, value: '2.66'},
        { id: 8, value: '3.04'},
        { id: 9, value: '3.42'},
        { id: 10, value:'3.8'},
        { id: 11, value: '4.18'},
        { id: 12, value: '4.56'}
    ];

var resetArray = $scope.Layer;

     $scope.changeData = function(value)
      {

         var coating = $scope.Layer;
         if(coating != null)
         {
                var j = coating.length;                                 
                while(j>0)
                {
                    j =j-1;
                    var make = coating[j]['id'];    
                    var present = 0;


                        if(make == value)
                        {                                                           
                          coating.indexOf(make);
                          coating.splice(j,1);                        
                        }

                }
         }
      }
      $scope.resetLayer =function()
      {
          $scope.Layer = restArray;

      }

});
</script> 

答案 2 :(得分:1)

您在此行中将错误分配给resetLayer变量

$scope.resetLayer -function(){}

这应该是

$scope.resetLayer =function(){};

享受!