Angular获取所有表单输入值

时间:2016-02-11 08:43:42

标签: javascript angularjs

我有这样的表格:

<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-submit工作正常,它返回个人资料值。 我的问题是如何才能获得基于表单的表单键和值而不是获取所有配置文件键?

我的console.log返回:

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

感谢。

1 个答案:

答案 0 :(得分:0)

你可以使用jQuery获取它:

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