【问题标题】:angular how input and select value return result without submit角度如何输入和选择值返回结果而不提交
【发布时间】:2021-12-27 12:41:55
【问题描述】:

angular 如何在不提交的情况下输入和选择值返回结果 我有货币转换器表格最新的 api

<form [formGroup]="newForm" (ngSubmit)="convert()">
        <div>

            <h1>
                Currency
            </h1>
            <div>

                <input type="number" formControlName="amount1">

                <select id="from" formControlName="from">
         <option value="" disabled>Choose currency</option>
        <option value="{{ m.code }}" *ngFor="let m of rates" >{{m.code}}</option>
    </select>
            </div>
            <div>

                <input type="number" [value]="name">

                <select id="to" formControlName="to">
         <option value="" disabled>Choose currency</option>
        <option value="{{ m.code }}" *ngFor="let m of rates" >{{m.code}}</option>
    </select>
            </div>
            <button type="submit" [disabled]="!newForm.valid"> Convert </button>
        </div>
    </form>

例如,要将美元转换为欧元,请在“amount1”中输入一个数字并将其返回给。如何在没有提交按钮的情况下自动返回结果,如果我更改数字,它会自动更改

【问题讨论】:

  • (ngModelChange) 监控变化

标签: javascript angular typescript currency converters


【解决方案1】:

您可以通过订阅 ngOnInit 方法中的 valueChanges 属性来检测表单值的变化。

 this.newForm.get('amount1').valueChanges.subscribe((val) => {
  this.name = val;
});

【讨论】:

    猜你喜欢
    • 2014-09-12
    • 1970-01-01
    • 2018-08-11
    • 1970-01-01
    • 2018-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多