简体   繁体   English

如何使用插值更改角度 6 中的变量?

[英]How do I change a variable in angular 6 using interpolation?

All I've been trying to do is change 5 to 6 using interpolation, but nothing I'm doing is working :(我一直试图做的就是使用插值将 5 更改为 6,但我所做的一切都不起作用:(

哑变量

component.ts
  x = 5;

  changeX(){
    this.x = this.x + 1;
  }

component.html组件.html

{{x}}

<div>
  wtf{{x.changeX}}
</div>

{{x}}

That's not how it works.这不是它的工作原理。 You'll have to somehow call changeX .你必须以某种方式调用changeX But you can only do that if there's any DOM event.但是只有在有任何 DOM 事件时才能这样做。 So I've added changeX as a click event handler to the div .所以我已经将changeX作为click事件处理程序添加到div Whenever the div is clicked, x will increment.每当单击divx都会增加。

{{x}}

<div (click)="changeX()">
  wtf
</div>

{{x}}

Here's a StackBlitz for it.这是一个StackBlitz

this works for me:这对我有用:

in the component:在组件中:

  @Output()
  private x = 5;

  public incX() {
    this.x++;
  }

in the template:在模板中:

{{x}}
<div>
  wtf{{incX()}}
</div>
{{x}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM