Angular: use an icon in place of text

Consider this scenario where a component.ts file looks like the following:

@Input() studentHeader?: BaseStudent;

 get studentText() {
        if(this.studentHeader) {
            return this.studentHeader.nr + ' - ' +
                this.studentHeader.firstname + ' ' +
                this.studentHeader.lastname + ' ' +
                this.studentHeader.gender + ' ' +
        }

        return '';
    }

        get iconStudent() {
            let icon = null;
            if (this.studentHeader) {
                switch (this.studentHeader) {
                        case (this.studentHeader.gender === 'FEMALE') :
                            icon = 'icon_female_i.svg';
                            break;
                        case (this.studentHeader.gender === 'MALE') :
                            icon = 'icon_male_i.svg';
                            break;
            }
            return icon;
        }
}

Imagine having an HTML template structured like so:

<div class="h-header">
    <div class="h-header-left">
        <div class="iconStudent"></div>
        {{studentText}}<br>
    </div>

Upon compilation, you see the output as:

1 - John Doe MALE

, which only displays the text and not the corresponding icon. Is there a way to show the icon instead?

Answer №1

Looks like it could be effective!

    <div class="h-header-right">
        <img [src]="iconProfessor" />
        {{professorText}}<br>
    </div>

Answer №2

Give this a shot

<div class="studentIcon"><img [src]="studentIcon" /></div>

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

Angular application not compatible with Spring Boot

I am attempting to run a frontend application alongside a Spring Boot backend. The Angular application functions correctly on localhost:4200 after using ng serve. I experimented with using ng build --prod, then moving the files from the dist folder to sr ...

typescriptCreating a custom useFetch hook with TypeScript and Axios

I have a query regarding the utilization of the useFetch hook with TypeScript and Axios. I came across an example of the useFetch hook in JavaScript, but I need help adapting it for TypeScript. The JavaScript implementation only handles response and error ...

Preventing event bubbling/propagation in custom events: a step-by-step guide

Incorporating a module-project from Github into my Angular project, I am able to resize the DOM elements that are displayed on top of a div functioning as a drawing board. To configure my initial rectangles, I am utilizing a combination of mouseDown - mou ...

Error encountered during Typescript compilation in Angular9 using Babylon4.1.0 - Unable to locate 'react' module or namespace 'JSX' not found

I am currently encountering compilation issues with Babylon4.1.0 within an angular9 app. It appears that the inspector is having trouble importing the internally used "react" module. To reproduce the issue: * Create a new angular9 app using the CLI * Add @ ...

What are the best practices for implementing MapLabel in an Angular project?

I need help displaying text on top of multiple polygons on a map. I've heard that Map Label can help with this, but I'm having trouble implementing it and can't find any NPM resources. Here is the Stack Blitz URL for reference: https://stac ...

How is it possible that this is not causing a syntax or compile-time error?

Oops! I made a mistake and typed : instead of = on line 2 of this code snippet. Why does Typescript allow this error? Isn't colon supposed to indicate a known Type for a property declaration? I'm pretty sure there's a reason encoded in the ...

Components in Angular 2 rc6 are failing to load

Since upgrading my APP to rc6, I've been experiencing some issues with component loading/rendering. In my APP, I categorize components into routing_components and util_components. Interestingly, the routing_components are working perfectly fine while ...

Will adding additional line breaks increase the overall length of the code?

Currently, I am immersed in a project involving Angular 4 and TypeScript. Recently, I came across a video showcasing how VSCODE can enhance the code's appearance. Intrigued, I installed the prettier plugin to achieve the same effect. Running this tool ...

Encountered an issue while attempting to assign a value to a property that is declared as [key in keyof T]

I am currently working on implementing a function that selects specific properties of an object. Here is the current function: const project = function<T>(object: T, projection: Projection<T>): Partial<T> { throw new Error("not imple ...

Utilizing string to access property

Is there a better way to access interface/class properties using strings? Consider the following interface: interface Type { nestedProperty: { a: number b: number } } I want to set nested properties using array iteration: let myType: Type = ...

How to Retrieve Superclass Fields in Angular 5 Component

I have a superclass that provides common functionality for components. export class AbstractComponent implements OnInit { public user: User; constructor(public http: HttpClient) { } ngOnInit(): void { this.http.get<User>(& ...

Managing the Sequence of HTTP Requests with RxJS

I have several REST requests that need to be executed in sequence, one after the other. At the completion of all requests, I also need to perform an additional action. I'm looking for a more efficient way to achieve this rather than cascading the re ...

Conceal the Button when the TextBox does not contain valid input

I'm trying to create a textbox with an email pattern that hides a span (click) if the pattern is invalid. I have the following code snippet in place, but it doesn't seem to work as expected: <input type="text" placeholder="Signup for Mailin ...

Showing JSON object in an Angular 2 template展示JSON对象在模

When I execute the following code: stanservice.categoryDetail(this.params.get('id')) .then((data) => { this.category = JSON.stringify(data.res.rows[0]); console.log(JSON.stringify(data.res.rows[0])); }) .catch((error) => { ...

Gatsby, in combination with TSC, does not properly transpile the rest operator

I'm attempting to integrate TypeScript with Gatsby following the guidelines provided here. However, I am encountering an issue where the tsc command is failing to transpile the spread (...) operator and producing the error shown below: WAIT Compili ...

What is the method to define a loosely typed object literal in a TypeScript declaration?

We are currently in the process of creating TypeScript definitions for a library called args-js, which is designed to parse query strings and provide the results in an object literal format. For example: ?name=miriam&age=26 This input will produce th ...

Is there a way to navigate directly to the code in a TypeScript type definitions index.d.ts file within Visual Studio Code?

When I command-click on the express() function, it takes me to its definition: const app = express(); In vscode, it leads me to this line in an index.d.ts file: declare function e(): core.Express; However, when I try to jump to the definition of e(), i ...

typescript create object with some properties using object literal

Is there a way to initialize an instance of a class with an object literal that doesn't contain all the elements in the class, but those present are part of the class? class Example{ text:string; number:number; displayResult(){return thi ...

Utilizing a Function's Parameter Type in TypeScript: A Beginner's Guide

Currently, I am creating a custom method that wraps around Angular's HttpClient service. I want users to have the ability to pass in options, but I am struggling to find the proper way to reference that type in my method parameter definition. For exa ...

How can I set up timers and display their outcomes?

I recently built a timer using RXJS: let timer1value = null; let timeFinish = 30; let finishDate = new Date(); return timer(1000) .pipe( takeWhile(() => new Date() < finishDate), finalize(() => { timeFinish = fini ...