Style will be applied to Angular2 when the object's ID exceeds 100

I am working with object markers that have different Id's assigned to them. Now, I am trying to apply additional styling when the id > 100.

Check out the code snippet below:

<span *ngIf="result.object.reference > 100" class="tooltip-data">
   {{ result.resource.display_name }}
</span>

But I am wondering, how can I incorporate extra styling such as style="left: 12px"?

Answer №1

To apply styles conditionally, you can use the following methods:

<span *ngIf="result.object.reference > 100" class="tooltip-data" 
    [style.left.px]="id > 100 ? 12 : 0">

or

<span *ngIf="result.object.reference > 100" class="tooltip-data" 
    [style.left]="id > 100 ? '12px' : '0px'">

If you only want to set a style when id > 100, you can use this approach:

<span *ngIf="result.object.reference > 100" class="tooltip-data" 
    [ngStyle]="getStyle(id)">
noStyle = {};
gT100Style = {left: '12px'};
getStyle(id) {
  return id > 100 ? this.gT100Style : this.noStyle;
}

Ensure that noStyle and gT100Style are defined outside of getStyle() to avoid issues with change detection.

Another option is:

<span *ngIf="result.object.reference > 100" class="tooltip-data" 
    [ngStyle]="id > 100 ? {left: '12px'} : {}">

Check out this Plunker example for reference.

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 7: Separate Views for Search Bar and Results

Two components have been developed: search-bar.component.ts: displayed in all views search.component.ts: responsible for displaying the results (response from a REST API) The functionality is as follows: whenever I need to perform a global search (produ ...

Problem encountered while implementing callbacks in redux-saga

I am facing a scenario in which I have a function called onGetCameras that takes a callback function named getCamerasSuccess. The idea is to invoke the external function onGetCameras, which makes an AJAX call and then calls getCamerasSuccess upon completio ...

Ways to categorize items retrieved from an HTTP request to the backend in Angular

When making a call to the backend using this http request: this.StudentEnrollment.getRecordsById(list.value.split(/[\r\n]+/)).subscribe(values => { this.studentObject = values; }); The studentObject is structured as shown below: { recor ...

Error: The System variable is not defined in the current context - Angular 2

Recently, I updated my angular version and now I am encountering errors in the console that are preventing my application from running smoothly. Despite having no errors in my terminal, this issue has been persisting for days and I can't seem to find ...

Regular expressions that identify text located at the conclusion of a URL

Although the title may not be entirely appropriate, my goal is to create a regex that will remove any trailing '/' at the end of a URL under certain conditions. For example: http://stackoverflow.com/questions/ask/ to http://stackoverflow.com/qu ...

Retrieve data from a URL using Angular 6's HTTP POST method

UPDATE: Replaced res.json(data) with res.send(data) I am currently working on a web application using Angular 6 and NodeJS. My goal is to download a file through an HTTP POST request. The process involves sending a request to the server by calling a func ...

What is the best approach for dynamically accessing or rendering Children within an Angular Component?

I am facing an issue with reusing a component called wrapper with different child components. I found some helpful resources such as this SO question and this article. However, these only address cases where the child component is known in advance. In my s ...

Unable to open modal window in Angular 14 micro-frontend application

I've been working on a micro front end project and running into some issues with opening modal popup windows. I've tried both the Angular material and bootstrap approaches, but ran into problems with both. With Angular material, the popup window ...

Utilize a web service within a service file and leverage it across multiple locations

I am seeking help with reusing a service function in different parts of my app, specifically to display a certain parameter ('title') in the HTML template. Firstly, I created a service: import { Injectable } from '@angular/core'; impo ...

Using ngFor and ngModel: What is the best way to add values to the beginning of the array I am looping through?

I am facing an issue with my array of elements in which users can edit, add, and delete complete array elements. Everything works smoothly until I try to add a value to the beginning of the array using the unshift method. Below is a test that showcases th ...

Stylishly incorporating components in higher-order components

Trying to enhance my component wrapper with styles using a higher order component has led to Typescript flagging an error with ComponentWithAdddedColors. type Props = { bg?: string; }; function withColors<TProps>( Component: React.ComponentType ...

Tips for navigating through a nested JSON object with loops

Is it possible to access the value of the Address object within an interface in Angular using *ngFor? export interface User { id: number; name: string; username: string; email: string; address: Address; } export interface Address { st ...

Take action once the Promise outside of the then block has been successfully completed

Presented below is the code snippet: function getPromise():Promise<any> { let p = new Promise<any>((resolve, reject) => { //some logical resolve(data); }); p.finally(()=>{ //I want do something when ou ...

Error: Unable to retrieve data - Angular2 application encountered a XHR error with status code 404 (

Hey there, I recently started working with angularjs and created a demo app using angular2. However, when I try to run the application, I encounter an error. Interestingly, even though the error mentions a 404 status code, if I visit the URL http://loc ...

Navigating with Angular 6 using routerlink in a child module with router-outlet specified in the parent component (app.component

I'm currently working with the RouterModule and encountering an issue with the routerLinks. The problem I am facing is that the routerLinks are not functioning properly (the anchor tags are not clickable). This issue arises because they are located w ...

How can an Angular4 unit test disregard an HTML tag from a third-party component?

I've exhausted my resources by checking the docs, SO, and attempting various solutions, but I am unable to make this work. The issue arises when writing a unit test for an angular4 application using karma/jasmine. The test is targeting a component tha ...

What is the process for changing the name of an element in Angular 2?

I'm feeling a bit lost here ... I need to implement the feature that allows users to edit the name of an element by simply clicking on a button. These elements are all stored in the Storage system. How can I achieve this in my current setup? File: ho ...

How to Retrieve Observable<Record<string, boolean>> Value with the Help of AsyncPipe

My service retrieves permissions for the currently logged-in user as a record. The necessary observable is declared in my TypeScript file as a member variable: permissionsRecord$: Observable<Record<string, boolean>>; When I call the service, ...

Angular 4 Web Application with Node-Red for Sending HTTP GET Requests

I am creating a unique service that utilizes Node-red to send emails only when a GET request is made to 127.0.0.1:1880/hello (node-red port), and an Angular 4 web app (127.0.0.1:3000) for client access. Upon accessing the /hello page from a browser, I rec ...

Utilize the global theme feature within React Material-UI to create a cohesive

I'm feeling a bit lost when it comes to setting up React Material-UI theme. Even though I've tried to keep it simple, it's not working out for me as expected. Here is the code snippet I have: start.tsx const theme = createMuiTheme({ ...