0

嗨,我正在尝试将下一个对象放在一起

$scope.passengers = { 成人 : [ {firstname: "name", lastname: "anothername"},{firstname: "name", lastname: "anothername"},{firstname: "name", lastname: "anothername"} ],孩子:[{firstname:“name”,lastname:“anothername”},{firstname:“name”,lastname:“anothername”},{firstname:“name”,lastname:“anothername”}],其他: “一些价值”};

在我的控制器中,我有以下内容

$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)
 }

在我的 HTML 页面中

<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> 

但是,当我打印变量“passenger”时,控制台显示:Object {extras: "someValue"}

4

1 回答 1

1

您的问题是ng-repeat创建了一个孤立的范围,因此您的输入字段没有反映在正确的范围内。extras这可以通过正确填写您的字段来进一步验证。

您应该在 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">

并为儿童区做同样的事情。

于 2015-09-26T01:07:46.293 回答