Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何通过选择“角度”选项来显示输入字段?_Angular_Angular Material - Fatal编程技术网

Angular 如何通过选择“角度”选项来显示输入字段?

Angular 如何通过选择“角度”选项来显示输入字段?,angular,angular-material,Angular,Angular Material,这里我有一个物料原生选择,在这个选择下拉列表中有两个选项(%and number)when%选项当时我想按%显示折扣输入字段,当我选择数字选项当时我想按数字显示折扣输入字段如何通过选择选项显示输入字段 <div> <mat-form-field> <input matInput placeholder="Enter Price" type="number" [(ngModel)]="newObj.newPrice" name="newPrice">

这里我有一个
物料原生选择
,在这个选择下拉列表中有两个选项(%and number)when%选项当时我想按%显示折扣输入字段,当我选择数字选项当时我想按数字显示折扣输入字段如何通过选择选项显示输入字段

<div>
  <mat-form-field>
    <input matInput placeholder="Enter Price" type="number" [(ngModel)]="newObj.newPrice" name="newPrice">
  </mat-form-field>

  <mat-form-field>
    <select matNativeControl>
      <option value="percentagewise">%</option>
      <option value="numberwise">Number</option>
    </select>
  </mat-form-field>

  <mat-form-field>
    <input matInput placeholder="Enter Discount By %" [(ngModel)]="newObj.newDiscountByPercentage" name="newDiscountByPercentage">
  </mat-form-field>

  <mat-form-field>
    <input matInput placeholder="Enter Discount By Number" type="number" [(ngModel)]="newObj.newDiscountByNumber" name="newDiscountByNumber">
  </mat-form-field>  
</div>

%
数

您可以使用
*ngIf
,但首先需要在
上定义
ngModel
,选择
以获得
双向绑定
,然后在
*ngIf
中使用它的变量

<mat-form-field>
    <select matNativeControl [(ngModel)]="discountType">
      <option value="percentagewise">%</option>
      <option value="numberwise">Number</option>
    </select>
  </mat-form-field>

  <mat-form-field *ngIf="discountType == 'percentagewise'" >
    <inputmatInput placeholder="Enter Discount By %" [(ngModel)]="newObj.newDiscountByPercentage" name="newDiscountByPercentage">
  </mat-form-field>

  <mat-form-field *ngIf="discountType == 'numberwise'" >
    <input matInput placeholder="Enter Discount By Number" type="number" [(ngModel)]="newObj.newDiscountByNumber" name="newDiscountByNumber">
  </mat-form-field>

%
数

您还应该在
component.ts
文件中定义
discountType
,以避免编译过程中出现错误。阅读有关
*ngIf
指令的更多信息您可以使用
*ngIf
,但首先您需要在
上定义
ngModel
以获得
双向绑定
,然后在
*ngIf
中使用它的变量

<mat-form-field>
    <select matNativeControl [(ngModel)]="discountType">
      <option value="percentagewise">%</option>
      <option value="numberwise">Number</option>
    </select>
  </mat-form-field>

  <mat-form-field *ngIf="discountType == 'percentagewise'" >
    <inputmatInput placeholder="Enter Discount By %" [(ngModel)]="newObj.newDiscountByPercentage" name="newDiscountByPercentage">
  </mat-form-field>

  <mat-form-field *ngIf="discountType == 'numberwise'" >
    <input matInput placeholder="Enter Discount By Number" type="number" [(ngModel)]="newObj.newDiscountByNumber" name="newDiscountByNumber">
  </mat-form-field>

%
数
您还应该在
component.ts
文件中定义
discountType
,以避免编译过程中出现错误。阅读有关
*ngIf
指令的更多信息