简体   繁体   English

Angular 4 中的 FontAwesome 微调器图标上的样式绑定未更新

[英]Style binding not updating on FontAwesome spinner icon in Angular 4

This seems like a silly problem, but after investigating and doing all kinds of tests, I don't understand why this happens.这似乎是一个愚蠢的问题,但经过调查和各种测试后,我不明白为什么会发生这种情况。

I have a Send button that should show a spinner while sending, and then go back to not showing it.我有一个发送按钮,应该在发送时显示一个微调器,然后返回不显示它。

<button (click)="sendCSV()" class="btn btn-primary btn-sm"><i class="fas fa-spinner fa-spin" [style.visibility] = "sendingEmails ? 'visible' : 'hidden'"></i>   Send</button>
{{sendingEmails}}

I added {{sendingEmails}} below the button to verify that the variable is changing properly, and it is.我在按钮下方添加了 {{sendingEmails}} 以验证变量是否正确更改,并且确实如此。 In my component, I have a sendingEmails var that is originally false, and:在我的组件中,我有一个原本为 false 的 sentEmails var,并且:

sendCSV() {

        this.sendingEmails = true;
        this.eventsService.sendGuestlistByEmail(this.eventId).subscribe(
            res => {
                this.sendingEmails = false;
                console.log(res);
            }, 
            err => {
                this.sendingEmails = false;
                console.log("Error.");
            }
        );
    }

The problem is that when I click send, the data binding will update properly (false-true-false), but the styles stay as if I had never clicked.问题是当我单击发送时,数据绑定将正确更新(假-真-假),但样式保持得好像我从未单击过一样。

In case it matters, I'm using Font Awesome 5 for the spinner.万一重要,我正在使用 Font Awesome 5 作为微调器。

I also tried我也试过

<i class="fas fa-spinner fa-spin" *ngIf="sendingEmails"></i>

This will lead to the spinner multiplicating every time I send the emails, and never actually disappearing after the first time.这将导致每次我发送电子邮件时微调器都成倍增加,并且在第一次之后实际上永远不会消失。

Any idea why this could be?知道为什么会这样吗?

Thank you all for your input and suggestions.谢谢大家的意见和建议。

It turns out that the problem was not Angular, but FontAwesome.事实证明,问题不是 Angular,而是 FontAwesome。 I recently upgraded to FontAwesome 5, which works with SVGs, instead of CSS.我最近升级到 FontAwesome 5,它适用于 SVG,而不是 CSS。 So, FontAwesome was turning my <i> into an SVG, and therefore, not updating properly.因此,FontAwesome 将我的<i>变成了 SVG,因此没有正确更新。

It was a very silly problem, but it might come up for people who were used to the old FontAwesome, so I wanted to leave my "solution".这是一个非常愚蠢的问题,但对于习惯了旧 FontAwesome 的人来说可能会出现,所以我想留下我的“解决方案”。

It's as simple as doing:这就像做一样简单:

<span [style.visibility] = "sendingEmails ? 'visible' : 'hidden'">
    <i class="fas fa-spinner fa-spin"></i>
</span>

try to use ChangeDetectorRef.detectChanges() to run change detection on the component尝试使用 ChangeDetectorRef.detectChanges() 在组件上运行更改检测

  constructor(private changeD: ChangeDetectorRef) {}

  sendCSV() {
   this.sendingEmails = true;
    this.eventsService.sendGuestlistByEmail(this.eventId).subscribe(
        res => {
            this.sendingEmails = false;
            this.changeD.detectChanges();
            console.log(res);
        }, 
        err => {
            this.sendingEmails = false;
            console.log("Error.");
        }
    );
  }

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

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