将隐藏的输入绑定到角度中的模型

Bind hidden inputs to model in angular

本文关键字:模型 绑定 隐藏 输入      更新时间:2023-12-13

我有以下表格:

<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,但在SaveRecord函数中没有得到record.usersIdrecord.userNameId

我哪里搞错了?

隐藏输入的值是正确的。

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

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

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

顺便说一句,在调用函数时,您不需要提及您的变量

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

您可以使用这样的东西:

<input type="hidden" ng-model="record.usersId" value="{{user.userId}}" ng-init="record.usersId=user.userId"/>
隐藏字段不支持双重绑定。

只需使用这个:

<input type="hidden" name="userId" value="{{user.userId}}"/> {{user.userId}}
<input type="hidden" name="UserNameId" value="{{user.userNameId}}"/> {{user.userNameId}}