Exploring the Power of Buttons in Angular 2

I have a scenario where I need to implement two different buttons in my template. The first button is as follows:

<button 
 [style.background-color]="service_rec.status == 
 'Online' ? 'green' : 'red'" class="btn btn-default">
              {{ service_rec.status }}
 </button>

The second button is displayed based on a condition using *ngIf and also has a click event attached to it. Here's the code snippet for the second button:

<button *ngIf="!!service_rec.servicecontrolled" 
  [style.background-color]="service_rec.controlled == 
  'true' ? 'green' : 'orange'"
  class="btn btn-warning"
  (click)="onPost(service_rec.title, service_rec.status, service_rec.id)">
  {{ service_rec.servicecontrolled | json | toOnOff }}
</button>

Additionally, there is a custom pipe called 'toOnOff' which transforms a boolean value into either 'Stop' or 'false'. Here is the implementation of the custom pipe:

 @Pipe({ name: 'toOnOff' })
  export class OnOffPipe implements PipeTransform {
  transform(value: boolean): string {
    return (!!value) ? 'Stop' : 'false';
  }
}

The next requirement is to create another custom pipe that will generate a 'Start' button instead of a 'Stop' button when the second button shows 'Offline' instead of 'Online'.

https://i.sstatic.net/YcJvH.png

Answer №1

Adding custom parameters to pipes is a useful feature. Take a look at this example:

{{ service_rec.servicecontrolled | json | toOnOff:'some value' }}
{{ service_rec.servicecontrolled | json | toOnOff:someProp }}

By doing this, you can access the parameter in the transform method of the pipe.

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

Importing/Requiring an External Module in Typescript Node using a Symbolic Link in the

I am in the process of migrating a Node + Express application to TypeScript and have encountered an issue with using external modules. Previously, I was utilizing the "symlink trick" to avoid dealing with relative paths. This is how it used to work withou ...

Creating a split hero section view using a combination of absolute/relative CSS techniques, Tailwind, and React

I'm in the process of creating a website using Nextjs, React, and TailwindCSS, and I aim to design a Hero section that resembles the one on the following website. https://i.sstatic.net/tq3zW.png My goal is to: Have a text title and buttons on the l ...

Trouble with line breaks in expandable rows of data grid causing clarity issues

Exploring the expandable rows feature of Clarity datagrid for the first time. I initially thought I could insert any HTML code within the collapsed view to show content. Using VMware's expandable datagrid: However, I am facing an issue where even wh ...

I am looking for unique values from a duplicate string that is separated by commas in TypeScript

Using TypeScript, I am trying to extract unique values from a list of comma-separated duplicate strings: this.Proid = this.ProductIdList.map(function (e) { return e.ProductId;}).join(','); this.Proid = "2,5,2,3,3"; The desired output is: this. ...

Is it possible to assign functions to each keystroke that does not correspond to a specific keybinding in Angular?

In Angular, there are events tied to keybindings or actions like (focus), (blur), (keydown), and more. You can bind specific keybinds to certain keys as well, such as (keydown.enter), (keydown.alt), etc. Is there a method to trigger an event only when it ...

Grab the content from a contenteditable HTML Element using React

Currently, I am developing an EditableLabel React component using Typescript in conjunction with the contenteditable attribute. My goal is to enable the selection of the entire text content when the user focuses on it, similar to the behavior showcased in ...

What causes the variation in typing behavior between specifying props directly on a component versus nesting them inside another prop?

Understanding the next component might be a bit tricky, so let's delve into it (Check playground): type Props<T> = { initValue: T, process: (value: T) => T } export const Input = <T,>({ initValue, process, }: Props<T>): ...

Discovering how to specify the type of a dynamically created object using 'for await' in TypeScript

for await (user of users) { ... } Encountered an issue: "error TS2552: Cannot find name 'user'. Did you mean 'users'?" Appreciate your assistance. ...

Angular Proxy is not passing the WebSocket Upgrade Header

When using the Angular CLI proxy by running ng serve --proxy-config proxy.conf.json the configuration looks like this: { "/api/*": { "ws": true, "secure": false, "target": "http://localhost:80", "logLevel": "debug" ...

Error: The function list.forEach does not exist within service.buildList [as project]

Today, I've been grappling with a challenging issue. As someone new to Typescript and Angular, I'm attempting to make a call to my backend API. However, when trying to populate an array for display, I keep encountering an error that says rawRegis ...

Angular Error: Issue: Unable to locate the specified column ID within the TS file

Within my application, I have a table containing multiple columns. I am attempting to populate it with the appropriate data, but upon opening the page, I encounter the error Could not find column with id "PublishedParty", despite it being present ...

FormGroup believes it is necessary even without specifying any validators

Trying to create a form that includes a nested FormGroup: this.form = this.formBuilder.group({ kitType: [ '', [ Validators.required, ] ], diagnosisCode: [ '', [ Validators.required, ] ], delayKitSh ...

Encountering Problems when Converting Vue Application from JavaScript to TypeScript with Vue and Typescript

Currently, I am in the process of converting a Vue project from JavaScript to TypeScript without utilizing the class-style syntax. These are the steps I took: I ran: vue add typescript I went through all my .vue files and: Indicated that TypeScript ...

Utilize node-pre-gyp to execute the installation with the option to fallback to building and specify the static

Attempting to install an Angular App into an existing project, encountering a dependency error during npm install. After cloning the existing Angular project, I am setting up my development environment. Note that I am working behind a proxy and firewall, ...

There are three possible interfaces for a functional component using React and Typescript

My goal is to develop a component that can utilize one of three interfaces based on the props passed to it. interface CommonProps { label: string; icon?: React.ComponentType; role?: string; } interface ButtonProps extends CommonProps { handleOnCl ...

Boost Page Speed with Angular

Currently, I am working on a project using Angular and encountered an issue with testing the page speed on GTmetrix. Despite generating the build with the command ng build --prod--aot, the file size is 1.9mb, leading to a low speed in GTmetrix's analy ...

What is the best way to add an item to an array with distinct properties?

I am currently working on creating an array with different properties for each day of the week. Here is what I have so far: const [fullData, setFullData] = useState([{index:-1,exercise:''}]) My goal is to allow users to choose exercises for a sp ...

Derive data type details from a string using template literals

Can a specific type be constructed directly from the provided string? I am interested in creating a type similar to the example below: type MyConfig<T> = { elements: T[]; onUpdate: (modified: GeneratedType<T>) => void; } const configur ...

The challenges of handling dynamic controls and reactive forms in Angular when editing, and ways to effectively update their values

I am currently working on creating an inline dynamic form using reactive forms. I have successfully added a new object and looped it in the DOM, but when I press the edit button, it doesn't work and produces errors in the console. Inside the object, t ...

Executing npm prepublish on a complete project

I am facing a situation with some unconventional "local" npm modules that are written in TypeScript and have interdependencies like this: A -> B, C B -> C C -> D When I run npm install, it is crucial for all TypeScript compilation to happen in t ...