知道为什么console.log('hey');
当我单击按钮时没有登录到浏览器控制台吗?
我试图显示/隐藏sub-component
if<button (click)="hey">Click me!</button>
被点击。
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;
}
}
应用程序组件.html
<button (click)="hey">Click me!</button>
<div *ngIf="flag">
<sub-component></sub-component>
</div>
索引.html
<body>
<my-app>Loading...</my-app>
</body>
你需要调用一个方法。
<button (click)="logThis('hey')">Click me!</button>
<div *ngIf="flag">
<sub-component></sub-component>
</div>
然后在您的组件中:
logThis(msg) {
console.log(msg);
}
问题是(click)
点击事件。你必须告诉他当那个事件触发时该怎么做。在这种情况下,我们调用logThis()
.
更新:
我没有看到您实际上声明了一个hey()
方法并且您只是忘记了您的()
.
我以为你想直接传递一个字符串。我的错!
无论如何,这可能对其他人有用!:D
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句