描述:用户填写完体温,需要实时验证体温是否合法,并给出提示

<input class="weui-input" type="number" placeholder="请输入体温"

[formControl]="temperatureInput" name="temperatureName"/>

import {FormControl} from '@angular/forms';

temperatureInput = new FormControl();

this.temperature = this.temperatureInput.value; // 获取温度值

this.temperatureInput.valueChanges

      .pipe(debounceTime(1000))

      .subscribe(v => {

        if (v < 35 || v > 45) { // 如果温度输入超出预期,则提示错误

          this.srv['warn']('温度填写有误', 3500);

          return this.temperatureInput.setValue('');

        }

        const str = v.toString();

        if (str.indexOf('.') > -1 && str.split('.')[1].length >= 2) {

          const val = parseFloat(v) - 0; // inputvalue值转换为浮点数;

          const txt = val.toFixed(1); // 当用户输入价格小数点超过2位数,强制变成小数点2

          this.temperatureInput.setValue(txt);

        } else {

          this.temperatureInput.setValue(v - 0);

        }

      });

posted on 2020-03-11 11:16  奇迹般若果  阅读(424)  评论(0编辑  收藏  举报