首页 > 解决方案 > 材质垫选择名称属性

问题描述

我正在研究一些简单的形式,将其从引导程序转换为材料。

尽管我正在使用 Angular 6,但表单在提交时以旧式发布(不使用角度表单)

<form method="post" action="http://api.example.com/submit" id="user_form">

   <mat-form-field>
      <input matInput placeholder="name" name="username">
   </mat-form-field>


   <mat-form-field>
      <mat-select placeholder="user-type" name="usertype">
         <mat-option value="type1">type1</mat-option>
         <mat-option value="type2">type2</mat-option>
      </mat-select>
   </mat-form-field>

   <button type="submit">submit</button>

</form>

为简单起见,我想保持这种方式,并且在提交此表单时不要使用任何 javascript(没有模板驱动的表单或反应式表单)。

input向 中添加name属性时效果很好,imput当我发布表单(单击提交按钮)时,它按预期发送到服务器。

至于mat-select,此数据不会在发布数据中发送到服务器。我猜前者是原生的inputwheremat-select是一个组件。

有没有办法使这项工作?(再次,在 TS 端不处理表单 POST)

标签: angularangular-material

解决方案


找到了答案。

只需select像这样使用本机:

<select matNativeControl placeholder="user-type" name="usertype" required>
  <option value="" disabled selected></option>
  <option value="type1">type1</option>
  <option value="type2">type2</option>
</select>

推荐阅读