Alter the class based on the incoming string from the rxjs stream

I have a stream that outputs strings, and based on these strings I want to apply certain classes to a specific tag:

  • If the string is "ok", add class "fa-check"
  • If the string is "loading", add classes "fa-spin" and "fa-spinner"
  • If the string is "error", add class "fa-times"

I tried using [class.className] in angular2, but it doesn't seem to be working for me.

@Component({
    selector: '[event-spinner]',
    template: `
            <i class="fa fa-2x" [class.fa-spinner]="icon == 'loading'" id="spin"></i>
  `
  // The code below also didn't work
  //directives: [NgClass],
  //template: `
  //<div class="col-xs-5">
  //  <i class="fa fa-2x" [ngClass]="{'fa-check': icon == 'ok', 'fa-spin': icon == 'loading', 'fa-spinner': icon == 'loading', 'fa-times': icon == 'error'}" id="spin"></i>
  //</div>
  //`
})
export class EventSpinner {
    icon: string = "ok";

    constructor(public eventsService: EventsService) {
        //eventsService.icons.subscribe((x) => {
        //    this.icon = x;
        //});
    }
}

Answer №1

@Component({
    selector: '[event-spinner]',
    template: `
<i class="fa fa-5x" [ngClass]="{'fa-check-circle': icon == 'ok', 'fa-spin': icon == 'loading', 'fa-cog': icon == 'loading', 'fa-times-circle': icon == 'error'}" id="spin"></i>
  `
})

Plunker example

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Restful: Numerous scenarios for a single resource (is it the same API endpoint?)

In this scenario, there are two different approaches to creating a user: If the user is created by an administrator, no password is provided initially. The user must choose a password on the activation page. If the user creates their own account, a passw ...

Leveraging non-React entities to seamlessly integrate components within a component hierarchy in React utilizing TypeScript

I am currently working on a React Typescript project where I am exploring the use of traditional polymorphism. Below is a simplified version of my project, where components are returned from vanilla Typescript objects rather than React components, allowing ...

What steps should I take to successfully compile my Typescript Webpack project without any errors?

Currently, I am attempting to convert only two .js files into .ts files within my webpack node.js project and then compile them (actions.ts and flux.ts). When I execute the command: webpack --progress --colors I encounter the following errors: 'use ...

When trying to update a form field, the result may be an

Below is the code for my component: this.participantForm = this.fb.group({ occupation: [null], consent : new FormGroup({ consentBy: new FormControl(''), consentDate: new FormControl(new Date()) }) }) This is th ...

Troubleshooting: Angular Application Fails to Launch Following Git Repository Cloning

After successfully completing the Angular Tour of Heroes tutorial with some tweaks, I attempted to push it to a GIT repo in VSTS and clone it onto another machine. However, upon cloning it to the new machine, I am encountering issues trying to get it up an ...

Merging Type-GraphQL and Typegoose through a Variety of Decorators

Using a combination of Type-GraphQl and Typegoose, I aim to streamline my data definitions by consolidating them into one source for both GraphQL schemas and Mongoose queries. Is it feasible to merge the two libraries in a way that allows me to describe bo ...

Is there a way to carry out tests on keydown events within Jasmine by specifying the keyCode within an Angular2+

I am working on a project where I need to trigger keydown events on an <input> field. Tools Used Karma v1.7.1 as the test runner Tests running on Chrome browser Using Angular 5 framework Any insights on how I can achieve this? ...

What steps should be taken to find a particular route when a user logs in for the first time?

I am currently working on an application using Angular 2+, Node.js, and Express.js that includes registration and login authentication functionality. How can I direct first-time users to a specific route upon login, but for all subsequent logins have them ...

Implementing Class-based Dependency Injection in Express

Incorporating Express into a TypeScript project has presented me with a particular scenario Here is my route file: ... import findAllUsersFactory from "src/factory/FindAllUsers"; routes.get("/users", findAllUsersFactory().handle); ... ...

Looking to categorize and sum the values within an array of objects using JavaScript?

I'm looking to optimize this response data within my Angular application. res=[ { "url": "/page1", "views": 2 }, { "url": "/page2", "views": 1 }, { "url": "/page1", "views": 10 }, { "url": "/page2", "views": 4 }, { "url": "/page3", "views": 1 }, ...

When making a GET request with Angular and NodeJS that includes parameters, only the response object is returned without any associated

Let's say I have a URL like 'localhost:3000/verify/a5d5sd', which I send to a user via email. When the user clicks on this link, I use the parameter (a5d5sd) in the link to check some conditions on the server and in my MongoDB database. The ...

Is there a way to update components in Angular without affecting the current URL?

I want to update a component without changing the URL. For example, I have a component called register. When I visit my website at www.myweb.com, I would like to register by clicking on sign up. How can I display the register component without altering the ...

"Learn the trick of converting a stream into an array seamlessly with RxJs.toArray function without the need to finish the

In order to allow users to filter data by passing IDs, I have created a subject that can send an array of GUIDs: selectedVacancies: Subject<string[]> = new Subject(); selectedVacancies.next(['a00652cd-c11e-465f-ac09-aa4d3ab056c9', ...

How can I place an icon on a specific location in a Highcharts map?

I am currently utilizing Highcharts to showcase maps within my application. I am aiming to achieve two specific functionalities: 1) Upon clicking on any area, I want that area to be highlighted in red {completed} 2) Upon clicking on any area, I intend f ...

Set the GridToolbarQuickFilter text box to have an outlined style in Material UI v5

How can I customize the appearance of the GridToolbarQuickFilter textbox, such as outlining it? Ideally, I would like to accomplish this through theme.tsx, but I am open to any suggestions. https://i.stack.imgur.com/H1Ojj.png I have experimented with var ...

Utilizing Embedded Jetty for Angular URL rewriting

While deploying a WebSocket and an Angular application with Jetty, everything works fine during development. However, I am facing an issue in production where refreshing the frontend or entering a URL results in a 404 error from the server indicating that ...

Navigate between tabs with ease for children

Setting up my routes has been a bit challenging. I created a listRoutes in my app-routing.module.ts with some parameters. const listRoutes: Routes = [ { path: '', component: MlsComponent, }, { path: 'vente', compon ...

having trouble retrieving 200 status code from Angular server response

Objective: I need to make certain changes to the record locally if the server responds with a 200 code. Problem: I am unable to retrieve the response code or access the 'message' attribute. This is the server response I receive from the HTTP ca ...

Contrasting the utilization of `typeof` with a constant and `enum` in TypeScript

Can you explain the distinction between using typeof with a constant and an enum in TypeScript? For example: const TYPE_A = 'a' const TYPE_B = 'b' type MyType = | typeof TYPE_A | typeof TYPE_B type Result = { name: string type ...

Neither of the elements within the ngIf statement is visible despite the fact that one of them should evaluate to true

I'm currently grappling with using ngIf to conceal a component's details until the necessary variable is set. During this waiting period, it should display a loading message. Despite my efforts to find a solution through online searches, I'v ...