【问题标题】:Angular - Unable to get operation to work in both waysAngular - 无法以两种方式进行操作
【发布时间】:2020-01-29 00:24:46
【问题描述】:

我正在使用 @HostListener("document:keyup", ["$event"]) 进行实时操作。 假设我有 3 个输入:A B 和 R(结果)。我想对 A+B 求和并在其输入中显示 R。 但我想修改 R 并让 B 被 R-A 修改。

在屏幕上:

输入 A:5

输入 B:3

输入 R:8

然后,手动修改 R 为 5 改变 B 的值。

输入 R:8 -> 5。 输入 B 应该变成 0。但它永远不会变成 0,因为只要我按下任何东西,函数就会计算 A+B 并在 R 上给出结果。 如何在不创建循环的情况下实现这一点?

 @HostListener("document:keyup", ["$event"])
  public operar(){

    this.gananciaEuros = 0;
    this.gananciaPorcentaje ="";
    this.resultado = "";
    this.calc.controls.total.setValue(0);
    if (!this.calc.controls.costo.value || !this.calc.controls.precioNeto.value) { //si la var está vacía
      this.calc.controls.total.setValue("Rellene ambos campos");
    }else{
      this.resultado = Number(Number(this.calc.controls.precioNeto.value) / Number(this.calc.controls.costo.value));
      this.gananciaEuros = Number((Number(this.calc.controls.precioNeto.value) - Number(this.calc.controls.costo.value)).toFixed(2));
      this.gananciaPorcentaje = String(((this.resultado - 1) * 100).toFixed(2));
      this.calc.controls.total.setValue(Number(this.gananciaEuros)/* +" (" + String(this.gananciaPorcentaje) +"%)" */);
      this.calc.controls.precioNeto.setValue(Number(this.gananciaEuros) + Number(this.calc.controls.costo.value))
    }
  }

html

<div class="divformulario">
    <form [formGroup]="calc">
        <div class="formsubconjuntovertical spacebetween">
         <div [innerHtml]="'Calcular Ganancia sobre precio' | uppercase | bold" "></div>
            <mat-form-field>
                <mat-label>Precio costo</mat-label>
                <input matInput type="number" formControlName="costo" />
            </mat-form-field>
            <mat-form-field  >
                <mat-label>Precio de venta neto</mat-label>
                <input matInput type="number" formControlName="precioNeto" " />
            </mat-form-field>
            <mat-form-field>
                <mat-label>Ganancia</mat-label>
                <input matInput type="number" formControlName="total" style="text-align: right;" "/><span matSuffix>€</span>
            </mat-form-field>
        </div>
    </form>
</div>

【问题讨论】:

  • 请发布您的代码。不看函数,我们不可能猜到哪里出了问题
  • 添加代码以供参考。

标签: angular


【解决方案1】:

您可以使用事件keyup 并将三个值(a、b、r)绑定到组件变量,而不是使用HostListener

这是html 文件的示例:

A
<input type="number" [(ngModel)]="aValue" (keyup)="changeFromA($event)"/>

B
<input type="number" [(ngModel)]="bValue" (keyup)="changeFromB($event)"/>

R
<input type="number" [(ngModel)]="rValue" (keyup)="changeFromR($event)"/>

在你的组件中:

aValue : number = 5;
bValue : number = 3;
rValue : number = 8;

changeFromA(_ : any) {
  this.rValue = this.aValue + this.bValue;
}

changeFromB(_ : any) {
  this.rValue = this.aValue + this.bValue;
}

changeFromR(_ : any) {
  this.bValue = this.rValue - this.aValue;
}

【讨论】:

  • 我会尝试这种方式。同时,由于我是 Angular 的新手,您在每个函数的括号中添加的下划线是什么?我知道“:any”是typescript需要的变量类型,但我不知道下划线是什么意思。
  • 这只是$event 参数,我可以写成changeFromA(event : KeyboardEvent)。我将此参数称为_,因为我知道我不会在我的函数中使用它。那只是一个名字。 :)
  • 我已经尝试了一个小测试 - 它可能按“keyup”的预期工作 - 但是,输入包含上下小箭头,通过单击它们来修改数字(1向上,1向下) .当我使用这些小按钮时,结果没有改变,可能是因为它们不是“keyup”。有什么建议吗? (编辑:我在没有通过 $event 的情况下这样做)
  • 在每个输入上添加(change)="changeFromX($event)",这样它也会检测到这个变化。
  • 效果很好。非常感谢,波卡丘。 (好尼克顺便说一句)。我可以问你最后一件事吗?我一直在尝试找到一种简单的方法来将焦点移动到输入键时的下一个输入。但是,我在互联网上找到的每一种方式都是我完全不理解的长代码实现。我想知道您(似乎很了解 Angular)是否知道一种方法可以轻松地改变这个焦点。我希望你不介意帮我解决这个额外的问题。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-06
  • 2011-08-23
相关资源
最近更新 更多