Sending data to a component through event binding

I am currently working on an angular project where I am looking to display a series of buttons. When each button is clicked, I want it to display the corresponding button number.

Is there a way to pass a specific value in the event binding that can be used within the component for making decisions?

<button (click)="clicked('I want to pass a value here')">Click </button>
<h1>The button number you entered is: {{buttonNumber}}</h1>

The values could range from 1 to n and will be utilized for decision-making in the controller class.

Answer №1

It is completely valid. What you need to do is create a function inside your component (named click in this case) and handle the $event that gets passed in.

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'sample',
  template: `
  <h1>The number you input is: {{number}}</h1>
  <button (click)="pressed('passing a value here')">Click</button>
  `
})

export class SampleComponent implements OnInit {
  constructor() { }

  ngOnInit() { }

  pressed(text) {
    // text is the parameter from the template
    console.log(text)
  }

}

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

Build an Angular wrapper component for the phone textbox functionality

Looking to transform the Phone Mask solution below into an Angular component. Does anyone have a method to accomplish this? * Any solution that results in a similar component for a Phone textbox will suffice. Mask for an Input to allow phone numbers? ht ...

Not all Angular Material2 styles are being fully applied

One issue I am encountering is that styles for components such as <mat-chip> or <button mat-raised-button> (the only ones I have found) are not working properly and appear gray by default. However, styles do apply correctly to the <mat-card& ...

React's componentDidUpdate being triggered before prop change occurs

I am working with the CryptoHistoricGraph component in my app.js file. I have passed this.state.coinPrices as a prop for this element. import React from 'react'; import axios from 'axios'; import CryptoSelect from './components/cry ...

How can one trigger a service method in nestjs through a command?

I am looking to run a service method without relying on API REST - I need to be able to execute it with just one command ...

Encountering issues with nested routes in Angular 2 causing errors

Currently, I am developing a web application using Angular 2 in an ASP.NET Core environment. The landing page is set at the base URL and has its own Layout file, Index page, and controller. My goal is to include a new section on my website located at /too ...

Replace the content of the HTML tag in the index.html file with a different HTML file located at a specific URL in an Angular application

Currently utilizing angular universal. The primary URL being: Upon opening this URL, the function res.render('index') is triggered, leading to the rendering of the index.html file, as depicted in the following code snippet. app.get('*' ...

Angular - Display shows previous and current data values

My Angular application has a variable called modelResponse that gets updated with new values and prints them. However, in the HTML, it also displays all of its old values along with the new ones. I used two-way data binding on modelResponse in the HTML [( ...

Is it possible to modify the color of a mat-progress-bar based on its status?

Within my project, I have implemented a mat-table containing a mat-progress-bar within each row. <mat-cell *matCellDef="let element"> {{element.id}} - {{element.address}} <mat-progress-bar mode="determinate" [v ...

In Angular, the data is displayed in the console but is not visible in the application

After successfully fetching data from the backend and seeing it displayed in the console https://i.sstatic.net/eRjli.png However, there seems to be an issue with rendering the data even though it is being recognized. Here's the relevant code snippet: ...

Navigating to the top of the page with Angular 5 tab scrolling

Currently, I am facing an issue with the Angular 5 Tabs. Whenever I switch from one tab to another, the page automatically scrolls to the top. Is there anyone familiar with a solution to this problem? <div class="row"> <div class="col-md-12"> ...

What is the best way to retrieve the dimensions of an Angular 5 Component prior to displaying it on the screen?

I am currently working on integrating a generic tooltip feature in Angular 5. In order to ensure proper positioning, especially centering relative to the target element, I need to obtain the width and height of the tooltip before it is rendered. While I h ...

Difficulty in connecting React to Node.js with the use of axios

Recently, I embarked on a project using React and Node to create an app that allows users to add people data to a database. The frontend is built with React and can be accessed at localhost:3000, while the backend, developed with Node, runs on localhost:33 ...

Having trouble getting Laravel and Angular to filter data by categories?

I am currently developing an ecommerce project using Laravel and Angular. I have products and brands associated with these products. In my function to retrieve the products in Laravel, I have used a nullable parameter like this: public function index($bran ...

How to extract a date from Mat-Datepicker without using moment library

I'm encountering an issue with Mat-datepicker in my Angular app. When I manually input the date into the field, it doesn't parse in the correct locale, but it displays correctly when selected from the month view. Recently, I made the switch from ...

Navigating through Objects in Angular 9

I am facing a challenge in Angular 9/Typescript while trying to iterate through the object response from my JSON data. Despite searching for solutions, I haven't found any that work for me. In my JSON, there is a section called "details" which contain ...

Angular 2: Enhancing communication between directives and host components

I am facing an issue with a component that has 'header', 'content', and 'footer' divs in its template. Within the content div, I have implemented a custom directive to check for overflow in the div element. This part is workin ...

Encountering challenges with periods in URL when utilizing a spring boot application alongside Angular in a live environment

Currently, I am in the process of developing a Spring boot + Angular application using JHipster and deploying it in a docker container with JIB. However, encountering an issue where URLs containing a dot are not functioning properly when accessed directly ...

Change the ddmmyy date string to dd/mm/yyyy format using TypeScript

When I use the date picker onchange method, the date is passed as a string like ddmmyyyy (20102020) to dd/mm/yyyy. I am unsure of how to convert the date format from ddmmyyyy to dd/mm/yyyy. In the CustomDateParserFormatter, the string needs to be converted ...

Ways to inform TypeScript of the potential return type when a generic's parameter can be either a string or a number

Let's take a look at a function with the following signature: function removeNumbersOrStringsElementsFromArray( targetArray: Array<number | string>, targetElementOrMultipleOfThem: number | string | Array<number | string> ): { upd ...

Customize your Loopback 4 OpenAPI with NSWAG by making filters optional and specifying data types

I am encountering an issue with the Loopback 4 filter on the generated endpoints being marked as required in my Nswag typescript file. I need it to be optional, but I am struggling to locate where this requirement is originating from. The endpoint from my ...