【问题标题】:HTML mouse event onclick is not working in simple Angular projectHTML鼠标事件onclick在简单的Angular项目中不起作用
【发布时间】: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


【解决方案1】:

Html 鼠标事件在 Angular 中不存在。在 Angular 中,HTML 鼠标事件的名称与 html 不同。我们可以参考这个链接列出Angular中的所有鼠标事件-https://dev.to/orahul1/angular-mouse-events-41l9

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-12
    • 2020-01-31
    • 1970-01-01
    • 2014-12-14
    相关资源
    最近更新 更多