2015-04-16 7 views
5

Un menu a discesa contiene un valore di array. Se seleziono un valore dal menu a tendina rimuoverà il suo valore dal suo array. Ma al clic del pulsante di reset dovrebbe resettare con vecchi valori .Qui è il mio codiceDopo aver rimosso un elemento dalla matrice usando la giunzione. non resettando. Il mio codice ha qualche errore

codice 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 codice controler

<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> 

utilizzando giunzione sto rimuovendo il valore selezionato discesa. ma il click del tasto la sua non è la reimpostazione

Grazie in anticipo

+0

'-funzione $ scope.resetLayer()' -> '$ scope.resetLayer = function()' – briosheje

risposta

2

Si dovrebbe prendere una copia della variabile, mentre si intialize/ottenere Layer dati

var copyOfLayer = angular.copy($scope.Layer); 

Poi mentre reseting che è necessario fare assegnare vecchio array anche la $scope.Layer è necessario riscrivere la funzione resetLayer al di sotto

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

Working Plunkr

+0

grazie per la tua risposta rapida e corretta – Athi

+0

@Athi felice di aiutarti, l Grazie :) –

1

L'errore è qui:

$scope.resetLayer -function() 

Dovrebbe essere

$scope.resetLayer =function() 

sto formattazione del codice:

<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> 
+0

ma questo non funziona per me – Athi

1

avete fatto un errore in questa linea in cui si stanno assegnando la funzione alla variabile resetLayer

$scope.resetLayer -function(){} 

Questo dovrebbe essere invece

$scope.resetLayer =function(){}; 

Enjoy!

+0

scusa questo è il mio errore – Athi

+0

Ho modificato la domanda – Athi

Problemi correlati