Angular(单击)在两个函数之间切换
Angular (click) switch between two functions
我有三个不同的按钮:A、B 和 C。A 和 B 几乎相同,但 类 不同,它们有 (click)="function"
; C 切换一个布尔值并切换它。如果布尔值为真,A 和 B 的函数必须为 function1(withParam)
,但如果为假,则必须为 function2()
.
这是html:
<ng-container *ngFor="let withParam of data[0]">
<ng-container *ngIf="withParam.hecha == true; else todo">
<button (click)="funcion" class="done"></button> //A
</ng-container>
<ng-template #todo>
<button (click)="funcion" class="todo"></button> //B
</ng-template>
</ng-container>
<button (click)="activaEdicion()"></button> //C
这是 ts:
funcion : string = "function1(withParam)"; //default value
activaEdicion(){
this.booleanVar = !this.booleanVar;
if(this.booleanVar){
this.funcion = "function2()";
}else{
this.funcion = "function1(withParam)";
}
}
但这行不通。还有另一个类似的问题,但答案是使用这个 <button (click)="this[funcion]()"></button>
,但它对我不起作用。看起来 (click) 需要实际功能,这就是它不起作用的原因,但我不知道该怎么做。我也不能使用 ts 文件中的函数,因为“withParam”与 HTML 相关,所以我需要将带有 Param 作为参数传递给 function1 及其相应的值,因此我不能这样做在 ts.
有什么线索吗?谢谢。
答案更新:
谢谢大家!我尝试了 @JackyShows 的回答,效果很好!
我敢肯定 @Panagiotis Bougioukos 的答案也能奏效,但另一个非常简单明了,所以我选择了那个。对于任何有同样问题的人来说,这是一个简单的 JavaScript if-else(我猜?我不知道我可以那样做)像这样:
<button (click)="booleanVar ? function2() : function1(withParam)" class="todo"></button>
再次感谢!
如果您改为在点击时使用 if 会怎样?我的意思是这样的:
<ng-template #todo>
<button (click)="booleanVar ? function2() : function1(withParam)" class="todo"></button>
我有三个不同的按钮:A、B 和 C。A 和 B 几乎相同,但 类 不同,它们有 (click)="function"
; C 切换一个布尔值并切换它。如果布尔值为真,A 和 B 的函数必须为 function1(withParam)
,但如果为假,则必须为 function2()
.
这是html:
<ng-container *ngFor="let withParam of data[0]">
<ng-container *ngIf="withParam.hecha == true; else todo">
<button (click)="funcion" class="done"></button> //A
</ng-container>
<ng-template #todo>
<button (click)="funcion" class="todo"></button> //B
</ng-template>
</ng-container>
<button (click)="activaEdicion()"></button> //C
这是 ts:
funcion : string = "function1(withParam)"; //default value
activaEdicion(){
this.booleanVar = !this.booleanVar;
if(this.booleanVar){
this.funcion = "function2()";
}else{
this.funcion = "function1(withParam)";
}
}
但这行不通。还有另一个类似的问题,但答案是使用这个 <button (click)="this[funcion]()"></button>
,但它对我不起作用。看起来 (click) 需要实际功能,这就是它不起作用的原因,但我不知道该怎么做。我也不能使用 ts 文件中的函数,因为“withParam”与 HTML 相关,所以我需要将带有 Param 作为参数传递给 function1 及其相应的值,因此我不能这样做在 ts.
有什么线索吗?谢谢。
答案更新:
谢谢大家!我尝试了 @JackyShows 的回答,效果很好! 我敢肯定 @Panagiotis Bougioukos 的答案也能奏效,但另一个非常简单明了,所以我选择了那个。对于任何有同样问题的人来说,这是一个简单的 JavaScript if-else(我猜?我不知道我可以那样做)像这样:
<button (click)="booleanVar ? function2() : function1(withParam)" class="todo"></button>
再次感谢!
如果您改为在点击时使用 if 会怎样?我的意思是这样的:
<ng-template #todo>
<button (click)="booleanVar ? function2() : function1(withParam)" class="todo"></button>