[英]How to toggle the text in <a> tag in angular 7
I have a button on tag with text "Unfreeze".我在标签上有一个带有“Unfreeze”文本的按钮。 I want to toggle it to "Freeze" on click on the button.
我想在单击按钮时将其切换为“冻结”。 Here is ny code:
这是纽约的代码:
<a class="btn btn-primary full-width" (click)="clickFreeze($event)">
<i class="fa fa-plus-circle"></i>Unfreeze</a>
private clickFreeze(event) {
console.log("event.srcElement.childNodes[1].textContent", event.srcElement.childNodes[1].textContent);
if(event.srcElement.childNodes[1].textContent =='Unfreeze'){
event.srcElement.innerText="Freeze and Save";
} else if(event.srcElement.innerText =='Freeze and Save'){
event.srcElement.innerText="Unfreeze";
}
}
For some reason it does not work.由于某种原因,它不起作用。
event.srcElement.childNodes[1].textContent
is equal to "Unfreeze" when I console it but it does not enter the if loop. event.srcElement.childNodes[1].textContent
在我控制台时等于“Unfreeze”,但它没有进入 if 循环。
I am not sure why you are using this approach, you can simply toggle text conditionally like this -我不确定你为什么使用这种方法,你可以像这样有条件地切换文本 -
.html .html
<a class="btn btn-primary" (click)="isFreeze = !isFreeze">
<i class="fa fa-plus-circle"></i>
{{isFreeze ? "Unfreeze" : "Freeze and Save"}}
</a>
.ts .ts
isFreeze: boolean = true;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.