0

app.component.ts

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'project';
  getName(name) 
  {
    alert(name);
  }
}

app.component.html

<button (click)="getName('tony')">click me</button>

I am new in angular and I am using version 12.1.4. Now, What happens here if I simply using click event without passing parameters inside getName() and alert("anything") then it works perfectly. But when I try to pass parameters inside the function and assign value in my click event function. Then it throws an error i.e. Parameter 'name' has implicitly had an 'any' type. Please help me to solve this.

2 Answers 2

2

You'll have to specify the type of the parameter.

 getName(name : any) 
  {
    alert(name);
  }

... or as a string.

Sign up to request clarification or add additional context in comments.

Comments

2

This is a TypeScript error you need to specify the type of the parameter you are passing to the method and also method return type if it has void otherwise.

In your case you are passing the name that means it would be string so add the string type i.e

getName(name : string): void {
   alert(name);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.