增量/减量(以角度表示)

时间:2019-10-07 17:40:07

标签: angular nativescript increment decrement

我要将数字从1递增到50。

我尝试使用此代码,如下所示,但是功能不起作用。

Demo

在html中,我有:

            <Button text="+" (tap)="plus()"></Button>
            <Button text="-" (tap)="minus()"></Button>

在ts中,我必须发挥作用,如下所示:

 plus() {
        var first: number = 1;
        var last: number = 50;
        let num = first + 1
        console.log("Value of num1 after increment ", first)
        if (num === last) {
            console.log('disable')
        } else {
            console.log('num plus', num)
        }
    }

    minus() {
        var first1: number = 1;
        var last1: number = 50;
        let num1 = first1 - 1
        console.log("Value of num1 after decr ", first1)
        if (num1 === last1) {
            console.log('disable')
        } else {
            console.log('num minus', num1)
        }
    }

请问有什么想法,如何增加/减少数字?

1 个答案:

答案 0 :(得分:0)

plus()minus()方法之外声明变量。我猜firstlast的数字是不变的。

app.component.ts:

export class AppComponent {
  name = "Angular";
  last1 = 50;
  last2 = -50;
  num1 = 0;
  num2 = 0;

  plus() {
    if (this.num1 === this.last1) {
      console.log("disable");
    } else {
      this.num1++;
      console.log("Value of num1 after increment ", this.num1);
    }
  }
  minus() {
    if (this.num2 === this.last2) {
      console.log("disable");
    } else {
      this.num2--;
      console.log("Value of num1 after decr ", this.num2);
    }
  }
}

app.component.html:

<Button (click)="plus()" class="btn">+</Button>
<Button (click)="minus()" class="btn">-</Button>

<p>Num1: {{num1}}</p>
<p>Num2: {{num2}}</p>