简体   繁体   中英

Save an array of objects in angular

Hi I'm trying to put together the next object

$scope.passengers = { adult : [ {firstname: "name", lastname: "anothername"},{firstname: "name", lastname: "anothername"},{firstname: "name", lastname: "anothername"}], child : [ {firstname: "name", lastname: "anothername"},{firstname: "name", lastname: "anothername"},{firstname: "name", lastname: "anothername"}], extras : "someValue"} ;

In my controller I have the following

$scope.passengers = {};
$scope.passengers.adult = [];
$scope.passengers.child = [];
$scope.numberAdult = 10;
$scope.numberChildren = 10;
Y las funciones
//Functions return an array null
$scope.getNumber = function(num) {
    num = parseInt(num);
    return new Array(num);   
};
$scope.savePassengers = function(product_id)
 {
    //for the moment only print the variable
    console.log($scope.passengers)
 }

In My HTML page

<form name="passengers-form" ng-submit="savePassengers(product.id)">        
    <div class="col-lg-12">

        <div class="form-horizontal">
            <div class="row">
                <div class="form-group"  ng-repeat="a in getNumber(numberAdult) track by $index">
                    <label class="col-sm-3 control-label">Adult {{$index + 1 }}</label>
                    <div class="col-sm-3">
        <input  required  ng-model="passengers.adult[$index].firstname" class="form-control" placeholder="First Name" type="text">
                    </div>

                    <div class="col-sm-3">
                            <input  required ng-model="passengers.adult[$index].lastname" class="form-control" placeholder="Last Name" type="text">
                    </div>
                </div>

                <div class="form-group"  ng-repeat="a in getNumber(numberChildren) track by $index">
                    <label class="col-sm-3 control-label">Child {{$index + 1 }}</label>
                    <div class="col-sm-3">
                            <input  required ng-model="passengers.child[$index].firstname"  class="form-control" placeholder="First Name" type="text">
                    </div>

                    <div class="col-sm-3">
                            <input  required  ng-model="passengers.child[$index].lastname" class="form-control" placeholder="Last Name" type="text">
                    </div>
                </div>


                <div class="form-group">
                    <label class="col-sm-3 control-label">Extras</label>
                    <div class="col-sm-6">
                            <textarea name="" ng-model="passengers.extras"class="form-control" id="" cols="30" rows="10"></textarea>
                    </div>
                </div>


                <div class="col-xs-9 col-sm-9 col-md-9 col-lg-9">

                    <div class="form-group buttons pull-right">                                                                 

                        <button class="btn btn-primary" id="guardar" type="submit">
                            <i class="fa fa-save"></i>Add to Cart ->
                        </button>                                                                                                                                       
                    </div>
                </div>                  

            </div>
        </div>

    </div>      
    <div class="clearfix"></div>
</form> 

But, when i print the variable "passenger" , the console show : Object {extras: "someValue"}

Your problem is that ng-repeat creates an isolated scope, so your input fields are not reflecting in the correct scope. This can further be verified by your extras field being properly fulfilled.

You should change these lines in your HTML:

<input  required  ng-model="$parent.passengers.adult[$index].firstname" class="form-control" placeholder="First Name" type="text">
...
<input  required ng-model="$parent.passengers.adult[$index].lastname" class="form-control" placeholder="Last Name" type="text">

And do the same thing for the children area.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM