2016-04-05 2 views
1

Ich habe eine Form, die zunimmt, wenn die Verwendung klicken Sie auf den Add Verkauf Buttonwie Eingabewert von der ng-repeat an den Controller bekommen

dies der html

<fieldset data-ng-repeat="choice in choices"> 
    <div class="form-group"> 
     <label for="name"> 
      Quantity : 
     </label> 
     <input type="text" class="form-control" id="choice.quantity" ng-model="choice.amount" /> 
    </div> 

ist, wie kann ich der Eingangswert in meinem Controller

appcat.controller("MainCtrl", ['$scope', '$location', function ($scope, $location) 
{ 
    // this controll the addition and removal 
    $scope.choices = [{ 'amount': '' }]; 

    // I want to set the input field 
     $scope.choice.amount = parseFloat(200* 4); // $scope.choice.amount and $scope.amount are not working 


    // this generate a form 
    $scope.addNewChoice = function() 
    { 
     var newItemNo = $scope.choices.length + 1; 

     $scope.choices.push({'amount': ''}); 

    }; 

    // this remove the form 
    $scope.removeChoice = function() { 
     var lastItem = $scope.choices.length - 1; 
     if ($scope.choices.length > 1) { 
      $scope.choices.splice(lastItem); 
     } 
    }; 
} 

wie kann ich den Eingangswert dieser Berechnung eingestellt parseFloat(200* 4); in meinem Controller

Antwort

0

Es könnte nur ein Tippfehler in dem Code Sie bei Stackoverflow kopiert haben, aber die Variable, die Sie einstellen sollten, ist:

$scope.choices[0].amount = parseFloat(200* 4) 

Oder noch kürzer:

$scope.choices = [{ 'amount': parseFloat(200* 4}]; 
1

Dazu müssten Sie ng-Modell verwenden:

Von AngularDocs:

Die ngModel Direktive bindet einen Eingang, wählen Sie Textbereich (oder Steuer individuelle Form) auf eine Eigenschaft auf den Umfang mit NgModelController, der von dieser Direktive erstellt und verfügbar gemacht wird.

-Link: https://docs.angularjs.org/api/ng/directive/ngModel

Verwandte Themen