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

Divs are not being organized into rows correctly due to issues with Bootstrap styling

I have implemented Bootstrap in my Angular application. The stylesheet link is included in my Index.html file as follows: <link rel="stylesheet" href="../node_modules/bootstrap/dist/css/bootstrap.css"> In addition to that, I have listed Bootstrap a ...

Is it possible to transform a ReadonlyArray<any> into a standard mutable array []?

At times, when working with Angular functions and callbacks, they may return a ReadonlyArray. However, I prefer using arrays in the traditional way and don't want to use immutable structures like those in Redux. So, what is the best way to convert a ...

Leveraging global attributes beyond Vue components - Vue 3

I have created custom service instances in main.ts app.config.globalProperties.$service1 = new Service1(); app.config.globalProperties.$service2 = new Service2(); While I can use these instances inside Vue components, I also want to be able to utilize the ...

Angular application table enclosed in a form, failing to capture checkbox values

I am working on an Angular 6 project and have a table with checkboxes. My goal is to select a row in the table by checking the checkbox, then click a "select" button to submit the data. However, my current HTML structure does not seem to be functioning as ...

Ways to recycle the table feature in angular2?

I am new to Angular2 framework and I am looking to efficiently reuse a single table component across my entire application. However, I am encountering challenges when it comes to displaying arrays in the table rows. How can I iterate through any type of ar ...

Managing Deactivated Elements in Ionic Web Edition

In my extensive ionic application that I deploy to both the Web and mobile platforms, a member of the testing team brought to my attention an issue regarding disabled elements. I have numerous elements such as buttons, checkboxes, inputs, etc., which can b ...

Leveraging Firestore Errors within Cloud Functions

Is it possible to utilize the FirestoreError class within Firebase cloud functions? The goal is to raise errors with a FirestoreError type when a document or field is not found in Firestore: throw new FirestoreError(); Upon importing FirestoreError using ...

Step-by-step guide on importing jquery-ui into Angular 4

I am looking to implement the toggle function from jquery-ui into my angular application. I have successfully installed jquery-ui using npm: npm install jquery jquery-ui Following the advice provided in this answer, I included the path in the angular-cli ...

Enter the newest addition of the node.js layer that bridges the gap between user interface and

Currently, I am in the process of creating a solution on AWS that relies on Cognito for managing users. To kickstart this project, I have referenced the SAAS QuickStart guide available at: SAAS QuickStart However, I am making a significant change by aim ...

Get rid of the Modal simply by clicking on the X mark

Objective: The modal should only be closed by clicking the X mark and not by clicking outside of it. Challenge: I am unsure how to resolve this issue when using the syntax code [config]="{backdrop: 'static'}" Additional Information: I am new ...

Looking for recommendations on the best tools to use for starting an Angular 2 project?

After successfully creating an Angular 2 web app on my computer, I encountered a problem when trying to build it for production. Instead of using my project files, angular-cli generated a "Hello World" app. The confusion arose because I was initially using ...

Unable to trigger the (click) event when adding new data in ANGULAR 4

Whenever I attempt to add the "tr tag", the (click) event is not functioning in Angular 4. Below is my code snippet. $('#time_table_table tbody').append('<tr data-id="1"><td>Sunday<a (click)="assign(1)">Assign</a>< ...

Adding TypeScript definition file to an npm package: A step-by-step guide

Is it possible to include typescript definitions (.d.ts files) in a pure javascript project without using TypeScript itself? I'm struggling to find any information on how to do this directly in the package.json. ...

Creating generic types for a function that builds <options>

I have encountered a situation in my application where I need to loop through an array to construct a list of <option> tags. To streamline this process, I am attempting to create a universal function. function optionValues<T, K extends keyof T> ...

Send information through a form by utilizing a personalized React hook

I'm having difficulty understanding how to implement a hook for submitting a form using fetch. Currently, this is what I have. The component containing the form: const MyForm = (): ReactElement => { const [status, data] = useSubmitForm('h ...

Created computed getter and setter for Vue models (also known as "props") using the syntax of Vue Class Component

After exploring the limitations of v-model in Vue 2.x in this Stack Overflow thread, I discovered a way to connect parent and child components using v-model. The solution proposed is as follows: --- ParentTemplate: <Child v-model="formData"> ...

What is the best way to deliver a downloaded document to a client using NodeJS Express?

I am facing an issue with downloading files from my FTP server using a button on my front-end website. The FTP server is password protected, and only I as the admin have access to the password. However, when the user clicks the download button, the file ge ...

How can I properly customize and expand upon a Material UI ListItem component?

I'm currently working with TypeScript version 3.4.5 and Material UI version 4.2. I have the following code snippet: interface MyItemProps { name: string; value: string; } function Item({ name, value, ...props }: ListItemProps<'li&apo ...

Discrepancy between code, output from ng build, and how the browser is

I am in the process of developing an Angular 13 library that includes two services and a group of models. These services consist of an authentication service and a client service. However, after building the library, I noticed some global variables missing ...

Transmit HTTP headers when making an HTTP request, similar to using Postman

Encountered an issue with Angular while attempting to send an HTTP POST request, resulting in the following error: Http failure during parsing for service.ts currentUse(SVF){ //let options = {headers: new HttpHeaders({'Content-Type': &apos ...