[英]How to pass $event object from my template to my component on Angular?
我想簡要了解一下事件綁定。
<button (click)="onClick($event)">click me</button>
但也像 object 一樣<button (click)="onClickObject">click me</button>
document.get.elementById('anyId')
並操作它們
import { Component, OnInit } from '@angular/core';
import { CoursesService } from '../courses.service';
@Component({
selector: 'app-courses',
template:`
<h1 id="h1id">{{ greeting }}</h1>
<button (click)="onClick($event)">click me</button>
`,
styleUrls: ['./courses.component.css']
})
export class CoursesComponent implements OnInit {
greeting ="";
onClick(event){
this.greeting = "hi my name is ..."
console.log(event)
}
constructor(){
}
ngOnInit(): void {
}
}
我收到 *Compiled with questions:X 錯誤
錯誤
src/app/courses/courses.component.ts:18:9 - 錯誤 TS7006:參數“事件”隱含地具有“任何”類型。
18 onClick(事件){*
在新版本的 Angular 中,我們需要傳遞聲明 function 的任何類內組件的參數類型。
<button (click)="onClick($event)">Greet for Employee</button>
這是模板的代碼。
onClick(event: any){ console.log(event); this.greeting="Welcome Chetan"; }
這是一個代碼類組件。 在這里,當我聲明 function onClick 時,我將事件視為任何類型的參數。 試試這個。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.