【发布时间】:2021-04-05 18:12:25
【问题描述】:
我是 Angular 框架的新手。我正在尝试在 app.components.html 文件中创建一个简单的按钮,单击该按钮后会将我带到 app.component.ts 文件中声明的函数。当我使用 (click) 事件属性时,一切正常,但是当我使用 onclick HTML 事件时,它不起作用。
app.component.html 是一个 HTML 文件,那么为什么 HTML 鼠标事件在这里不起作用?有人请在这里帮忙。 app.component.html 文件 -
<input type="button" value="Click Me" onclick="getName('AAKASH')"/>
app.component.ts 文件 -
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
// We can declare properties here
export class AppComponent {
title = 'blog';
getName(name) {
alert(
"button has been clicked" + name + " "
)
}
}
【问题讨论】:
-
onclick在 Angular 中不存在。您应该使用(click)="getName('...')"事件发射器。 -
标签: javascript html angular