Angular 为什么单击按钮时不调用组件函数
知道为什么Angular 为什么单击按钮时不调用组件函数,angular,typescript,Angular,Typescript,知道为什么console.log('hey') 我正在尝试显示/隐藏子组件如果单击我被单击 app.component.ts import { Component } from '@angular/core'; @Component({ selector: 'my-app', templateUrl: './app.component.html' }) export class AppComponent { public flag = false; public hey() {
console.log('hey')单击按钮时,浏览器控制台中是否未记录代码>
我正在尝试显示/隐藏子组件如果单击我代码>被单击
app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html'
})
export class AppComponent {
public flag = false;
public hey() {
console.log('hey');
this.flag = true;
}
}
app.component.html
<button (click)="hey">Click me!</button>
<div *ngIf="flag">
<sub-component></sub-component>
</div>
点击我!
index.html
<body>
<my-app>Loading...</my-app>
</body>
加载。。。
您需要调用一个方法
<button (click)="logThis('hey')">Click me!</button>
<div *ngIf="flag">
<sub-component></sub-component>
</div>
问题是,(点击)
是点击事件。你必须告诉他当事件触发时该怎么做。在本例中,我们调用logThis()
更新:
我没有看到您实际上声明了一个hey()
方法,只是忘记了()
我以为你是想直接传递一个字符串。我的错
无论如何,这对其他人可能有用!:D点击我代码>缺少括号。
logThis(msg) {
console.log(msg);
}