在AngularJS中提交隐藏表单信息的正确方法是什么?

时间:2015-02-16 10:07:06

标签: angularjs

我正在一个用户可以提交几种不同表格的网站上工作。我想包括提交的表单类型(基本,高级,其他)。我已经读过使用AngularJS的隐藏字段是可能的,但不推荐。我没有找到解决问题的方法,而是更愿意做正确的事情。提交不需要向用户显示但应包含在提交中的信息的正确方法是什么?

以下是表单的HTML:

<form name="myForm">
<div class="control-group">
    <label>Name</label>
    <input type="text" name="name">
</div>

<label>Description</label>
<textarea name="description"></textarea>
<button ng-click="editProject.save()" class="btn btn-primary">Save</button>

 <!--{{formselection}}-->

</form>

1 个答案:

答案 0 :(得分:0)

如果使用ng-model,隐藏字段位于对象内存上下文中,则只显示用户填充的数据。

HTML:

<form name="myForm">
      <div class="control-group">
          <label>Name</label>
          <input type="text" name="name" ng-model="form.name">
      </div>

      <label>Description</label>
      <textarea name="description" ng-model="form.description"></textarea>
      <button ng-click="editProject.save(form)" class="btn btn-primary">Save</button> <!-- Pass the form to the method in controller-->

    </form>

控制器:

 $scope.form = {
    hiddenField1: "anyValue",
    hiddenField2: "anotherValue"
  };

  $scope.editProject = {save: function(form){
    //http request given the form, this contain the name, description and the two hidden field

  }}

http://plnkr.co/edit/fjnGc4Q4f8ZfwhupkOdR