角度获取所有表单输入值

Angular get all form input value

本文关键字:表单 输入 获取      更新时间:2023-09-26

我有这个表格:

<form class="form-horizontal" role="form" name="form" ng-submit="update(profile)">
    <div class="form-group">
        <label class="col-md-2 control-label">Facebook</label>
        <div class="col-md-10">
            <div class="input-group">
                <input type="text" ng-model="profile.facebook"  class="form-control" placeholder="Facebook" />
                <span class="input-group-btn">
                    <button ripple-button color="facebook" icon="facebook"></button>
                </span>
            </div>
        </div>
    </div>
    <div class="form-group">
        <label class="col-md-2 control-label">Instagram</label>
        <div class="col-md-10">
            <div class="input-group">
                <input type="text" ng-model="profile.instagram"  class="form-control" placeholder="Instagram" />
                <span class="input-group-btn">
                    <button ripple-button color="instagram" icon="instagram"></button>
                </span>
            </div>
        </div>
    </div>
</form>

我的控制器:

Profile.get((resp)=>{
     $scope.profile = resp.result;
});
//resp.result is: {"id":"Hf561b6fd32aec6ea","name":"Example","nickname":"Example 1", "facebook":"example","instagram":"example"}
$scope.update = (profile) =>  {
    console.log(profile);
}

这是工作流程:首先,我将从服务器获取配置文件并放置所有ng-model profile.*值。用户可以编辑值并提交。

问题:我想一次从表单中获取所有值。我的 ng 提交工作正常,它返回配置文件值。我的问题是我如何仅获取基于表单的表单键和值,而不是获取所有配置文件键?

我的控制台.log返回以下内容:

{"id":"Hf561b6fd32aec6ea","name":"Example","nickname":"Example 1", "facebook":"thisischange","instagram":"thisischange"}

谢谢。

你可以

使用jQuery获取它:

var object = {};
$('.form-horizontal').serializeArray().forEach(pair) {
    object[pair.name] = pair.value;
});