1

I have a json file contains a icons object like this:

"icons" : {
        "logo" : "fa fa-caret-down",
        "search" : "fas fa-search",
        "bell" : "far fa-bell"
    }

In my component.ts, I am able to get this icons object using a service successfully like this:

navIcons: object;
   getNavIcons(): void {
    this.navbarService.getNavIcons()
    .subscribe(navIcons => {
      this.navIcons = navIcons;
      console.log(this.navIcons)
    //   icons : {
    //     logo : "fa fa-caret-down",
    //     search : "fas fa-search",
    //     bell : "far fa-bell"
    // }
    });
  }

How can I bind thoes class in the component.html template? I tried like this:

 <i id="logo" [ngClass]="['navIcons.logo']" class="nav-icon"></i>

but it failed. I also have another calss: nav-icon need to added into this logo? Thank you in advanced!

1 Answer 1

1

Try this ngClass syntax :

[ngClass]="navIcons?.logo"

Since navIcons is initially undefined, use the safe navigation operator ?. to prevent the exception when trying to access the logo property.

Sign up to request clarification or add additional context in comments.

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.