在角度和地图绑定中将新值推送到现有地图

时间:2015-11-15 08:22:17

标签: javascript angularjs angularjs-scope

Angularjs代码。

$scope.model;

我在角色中定义了我的新对象,如下面的

$scope.newRelation = {
    relationType : null,
    relatedModel : null
};

HTML

         <div class="table-responsive">
                <table class="table">
                    <tr>
                        <td><select class="form-control"
                            ng-model="newRelation.relationType" required
                            ng-options="modeltype as modeltype for modeltype in modeltypes"></select>
                        </td>
                        <td><select class="form-control"
                            ng-model="newRelation.relatedModel" required
                            ng-options="model.name as model.name for model in models"></select>
                        </td>
                        <td>
                            <button type="button" class="btn btn-default"
                                ng-click="addRelation()">Add Relation</button>
                        </td>
                    </tr>
                </table>
            </div>

角度代码

$scope.addRelation = function()
{
  $scope.model.relations.push($scope.newRelation);
};

当我点击表单时保存模型。后端的值是空的。任何线索?如果我需要提供更多信息,请告诉我

1 个答案:

答案 0 :(得分:0)

您直接将对象分配给列表。

它会继续参考。

制作副本

var obj=angular.copy($scope.newRelation);

然后推它

 $scope.model.relations.push(obj);