最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to toggle the text in <a> tag in angular 7 - Stack Overflow

programmeradmin2浏览0评论

I have a button on tag with text "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.

I have a button on tag with text "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.

Share Improve this question edited Nov 7, 2019 at 10:12 Dino 8,31213 gold badges49 silver badges93 bronze badges asked Nov 7, 2019 at 10:08 Yogesh MaliYogesh Mali 2237 silver badges17 bronze badges 3
  • <a class="btn btn-primary full-width" (click)="clickFreeze($event)"> <i class="fa fa-plus-circle"></i> Unfreeze </a> – Yogesh Mali Commented Nov 7, 2019 at 10:09
  • 2 do not fiddle with the DOM element directly in the model. – Krishna Prashatt Commented Nov 7, 2019 at 10:22
  • Possible duplicate of How to change Anchor Tag text on Click in angular 2 – dasunse Commented Nov 8, 2019 at 4:37
Add a ment  | 

1 Answer 1

Reset to default 14

I am not sure why you are using this approach, you can simply toggle text conditionally like this -

.html

<a class="btn btn-primary" (click)="isFreeze = !isFreeze">
  <i class="fa fa-plus-circle"></i>
  {{isFreeze ? "Unfreeze" : "Freeze and Save"}}
</a>

.ts

isFreeze: boolean = true;
发布评论

评论列表(0)

  1. 暂无评论