Bind hidden inputs to model in angular

喜你入骨 提交于 2019-11-27 12:00:10

问题


I have following form:

<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>

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

Where am i making mistake?

values of hidden inputs are correct.


回答1:


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>



回答2:


You can use something like this:

<input type="hidden" ng-model="record.usersId" value="{{user.userId}}" ng-init="record.usersId=user.userId"/>



回答3:


Hidden field does not support double binding.

Just use this:

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


来源:https://stackoverflow.com/questions/20258994/bind-hidden-inputs-to-model-in-angular

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!