本文介绍了将隐藏输入绑定到角度模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下表格:

<form name="frmInput">

    <input type="hidden" ng-model="record.usersId" value="{{user.userId}}"/>
    <input type="hidden" ng-model="record.userNameId" value="{{user.userNameId}}"/>
    <label for="fileNo">AccountId</label>
    <input id="fileNo" ng-model="record.fileNo" required/>
    <label for="madeSad">MadeSad</label>
    <input id="madeSad" ng-model="record.madeSadNo" required/>

    <button ng-disabled="!frmInput.$valid" ng-click="SaveRecord(record)">Accept</button>

</form>

我在 SaveRecord 函数中得到 record.fileNorecord.madeSadNo 但我没有得到 record.usersIdSaveRecord 函数中的 code> 和 record.userNameId.

I get record.fileNo and record.madeSadNo in SaveRecord function but i don't get record.usersId and record.userNameId in SaveRecord function.

我哪里出错了?

隐藏输入的值是正确的.

values of hidden inputs are correct.

推荐答案

隐藏表单域不是 Angular 的方式.您根本不需要隐藏字段,因为所有范围变量(不在表单中)都可以作为隐藏变量.

Having hidden form fields is not the Angular way. You don't need hidden fields at all, as the all the scope variables (which are not in the form) can be taken as hidden variables.

至于解决方案,在提交表单时,只需使用用户"填充对象记录":

As for the solution, while submitting the form, just populate the object 'record' with 'user':

function SaveRecord(){
  $scope.record.usersId = $scope.user.userId;
  $scope.record.userNameId = $scope.user.userNameId;
  http.post(url, $scope.record);
}

作为旁注,您无需在调用函数时提及您的变量:

As a side note, you do not need to mention your variable while calling the function:

<button ng-disabled="!frmInput.$valid" ng-click="saveRecord()">Accept</button>

这篇关于将隐藏输入绑定到角度模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 22:08