Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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',
    ]
}
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

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

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

here is a PLUNKER...

share|improve this answer
    
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?? –  VishwaKumar Mar 13 at 12:36
    
you can check validation of form if it is ok for you? –  wickY26 Mar 13 at 12:51
    
sorry dint get what you saying –  VishwaKumar Mar 13 at 13:16
    
@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...] –  wickY26 Mar 13 at 13:26
    
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 –  VishwaKumar Mar 13 at 17:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.