how to get input value from the ng-repeat to the controller Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of how to get input value from the ng-repeat to the controller without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I have a form that increases when the use click on the add sale button

this is the 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>

how can I get the input value in my 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);
        }
    };
}

how can I set the input value to this calculation parseFloat(200* 4); in my controller

Answer

It might just be a typo in the code you’ve copied to stackoverflow, but the variable you should set is:

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

Or even shorter:

$scope.choices = [{ 'amount': parseFloat(200* 4}]; 
We are here to answer your question about how to get input value from the ng-repeat to the controller - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji