在Angular中,你可以使用事件绑定来调用一个函数,当用户点击或按下回车键时触发。
下面是一个示例代码:
在HTML模板中:
在组件中的Typescript代码中:
import { Component } from '@angular/core';
@Component({
selector: 'app-my-component',
templateUrl: './my-component.component.html',
styleUrls: ['./my-component.component.css']
})
export class MyComponent {
myFunction() {
// 在这里编写你的函数逻辑
console.log('函数被调用!');
}
}
在这个示例中,当用户点击按钮时,或者在输入框中按下回车键时,myFunction()
函数将会被调用。你可以根据自己的需求修改函数的逻辑。