1

HTML

    <span [ngClass]="{
                       'fas fa-star' : isSelected,
                       'far fa-star' : !isSelected
                     }"
          (click)="OnClick()">
    </span>

typescript

import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';


@Component({
  selector: 'app-one-component',
  templateUrl: './one-component.component.html',
  styleUrls: ['./one-component.component.css']
})
export class OneComponentComponent implements OnInit {

@Input() isSelected : boolean;
@Output() change = new EventEmitter();
  constructor() { }

  ngOnInit() {
  }
  OnClick(){
    this.isSelected= !this.isSelected;
    this.change.emit(this.isSelected);
  }

}

while clicking icon partial class were loaded like fas or far after space no class was added.

1
  • Interesting, seems like a bug, the common thing disappears in such expressions. The same expression work fine, if all the class name are unique. May be you have to change it like the answers below. Commented Jan 26, 2019 at 3:59

2 Answers 2

1

If you’ll have the class “fa-star” in both scenarios, you can just do something like this:

<span [ngClass]=“{‘fa-star’:true, ‘fas’: isSelected, ‘far’: !isSelected}”></span>
Sign up to request clarification or add additional context in comments.

Comments

0

HTML

<span class="fa-star" [ngClass]="{
                   'fas' : isSelected,
                   'far' : !isSelected
                 }"
      (click)="OnClick()">
</span>

or

<span class="" [ngClass]="{
                   'fas' : isSelected,
                   'far' : !isSelected,
                   'fa-star' : true
                 }"
      (click)="OnClick()">
</span>

Class binding can be done as above.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.