html - 点击功能上的表格行在Angular7中不起作用
问题描述
每当我单击 HTML 表中的一行时,我都会尝试调用一个函数,但它不起作用并且没有给出任何错误。
我已经尝试了几件事,在线代码显示我尝试它的方式应该可以工作。
我的代码如下:
html:
<h2>Userstories</h2>
<button (click)="onNewStory()">New Story</button>
<table>
<tr *ngFor="let story of userstories" ng-click="onUpdate(story)" [class.selected]="story === selectedStory">
<td>{{story.id}}</td>
<td>{{story.name}}</td>
<td><button ng-click="onUpdate(story)">Click me!</button></td>
</tr>
</table>
<app-userstory-detail [story]="selectedStory"></app-userstory-detail>
我的 .ts 看起来像这样:
selectedStory: UserStory;
onNewStory() {
var newStory = {id:this.userstories[this.userstories.length-1].id+1, name:"", description:"Deze is nieuw", status:"open", owner:USERS[1]};
this.userstories.push(newStory);
this.selectedStory = newStory;
}
onUpdate(userstory: UserStory): void {
this.selectedStory = userstory;
console.log(this.selectedStory);
}
目前,当我尝试调用 onUpdate 方法时,我的控制台没有打印日志。预期的结果是在日志中看到一些输出,但我不知道为什么它没有触发 onUpdate 方法。
解决方案
在 Angular 7 中,您可以使用(click)
类似于ng-click
Angular JS 的。
尝试:
<h2>Userstories</h2>
<button (click)="onNewStory()">New Story</button>
<table>
<tr *ngFor="let story of userstories" (click)="onUpdate(story)" [class.selected]="story === selectedStory">
<td>{{story.id}}</td>
<td>{{story.name}}</td>
<td><button (click)="onUpdate(story)">Click me!</button></td>
</tr>
</table>
<app-userstory-detail [story]="selectedStory"></app-userstory-detail>
推荐阅读
- python - 在 for 循环中重命名读取的数据
- javascript - ReactJS:如何在 Tree View Node Material UI 中扩展和显示 API 调用?
- c++ - C++ 类型提升低于 int
- hexo - Hexo 注入 html
- jenkins - 我正在尝试使用 ZAP 集成在 jenkins 中创建一个自由式项目
- python - 更新 - 进一步的并发症 - ImportError: `load_weights` 需要 h5py
- http - 未创建对象的 Http 状态
- android - 如何将列表视图放入具有最大高度限制的约束布局中?
- android - 小米红米Note8 Pro及以上蓝牙“附加设置”
- python - 优化python代码蒙特卡罗模拟