2017-07-27 5 views
-1

Ich habe ein Problem mit ng-repeat und [$ index]. Mein Problem ist, dass ich Werte von allen Inputs möchte (schaue auf Fiddle und klicke auf "+"), speichere in einem Array und speichere als nächstes in der Datenbank. Das Problem ist ganz am Anfang, dass ich keine Eingabewerte in ein Array einfügen kann (das erste Array wird nicht in das Array eingefügt). Ich versuche Verwendung:Einfügen von Werten in einem Array

ng-repeat="input in inputs track by $index" 

Geige: https://jsfiddle.net/j6uwhb6v/4/

Antwort

1

Es funktioniert nicht, weil der erste Eingang außerhalb der ng-Repeat ist, damit es nicht einen $ index hat ... Hier ist eine einfache Lösung:

HTML:

<div ng-app="productController" ng-controller="productCtrl" class="row-fluid"> 
     <div class="col-md-12"> 
      <div class="form-group"> 
       <div class="input-group mb-2 mr-sm-2 mb-sm-0"> 
        <div class="input-group-addon"> 
         <i class="fa fa-pencil"></i> 
        </div> 
        <input ng-disabled="product.disabled" type="text" ng-model="description[0]" name="description" class="form-control" placeholder="Description..." required> 
        <div class="input-group-addon"> 
         <a ng-click="addfield()" class="add-field"> 
          <i class="fa fa-plus"></i> 
         </a>       
        </div> 
       </div> 
       <div ng-repeat="item in inputs track by $index"> 
        <div class="input-group mb-2 mr-sm-2 mb-sm-0 repeat-input"> 
         <div class="input-group-addon"> 
          <i class="fa fa-pencil"></i> 
         </div> 
         <input ng-disabled="product.disabled" type="text" name="description" ng-model="description[$index+1]" class="form-control" placeholder="Description..." required> 
        </div> 
       </div> 
      </div> 
     </div> 
     {{description}} 
    </div> 

JavaScript:

$scope.inputs = []; 
$scope.addfield = function(){ 
    $scope.inputs.push({}) 
} 
$scope.description = []; 

Arbeitscode: JSFiddle

+0

ja! es hilft mir sehr –

Verwandte Themen