0

I have the following form

<div ng-controller="Formctrl">
<form ng-submit="getNames(pd)">
    <fieldset>
        <div class="row">
        <section class="col col-4" ng-repeat="n in [] | range:5">
        <label class="input">
            <input type="text" placeholder="Names" ng-model="pd.names[$index]" >
        </label>
        </section>
        </div>
    </fieldset>
</form>
</div>

Basically i want to bind the names in an array form submission. But i am getting the following error

TypeError: Cannot set property '0' of undefined

Here is my Controller

angular.module('myApp')
    .controller('FormCtrl', function($rootScope,$scope){
        $scope.pd = {};
        $scope.getNames = function() { console.log($scope.pd); };

    });

I need the output something like below. How do i achieve it??

{
    names: [
        'Name 1',
        'Name 2',
        'Name 3',
        'Name 4',
    ]
}

1 Answer 1

1

Hey You just try to reach an undefined element just define names in controller...

$scope.pd = {names : []};

here is a PLUNKER...

Sign up to request clarification or add additional context in comments.

5 Comments

Oh it works! Thanks, but when i erase the submited value from textbox a null value is still left. cant i remove/ reset the names??
you can check validation of form if it is ok for you?
sorry dint get what you saying
@VishwaKumar you can watch and remove element from your array but I don't think it is a best solution, I wonder what you want to get when form is submitted we can focus that for another solution...]
Well , we have to make a check if the array value was present or no in a loop on the server side..wanted to just avoid that step

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.