Html 单击表格行功能在Angular7中不起作用
每当我在HTML表中单击一行时,我都试图调用一个函数,但它不起作用,也没有给出任何错误 我尝试过几种方法,在线代码显示我的尝试方式应该是有效的 我的代码如下: Html:Html 单击表格行功能在Angular7中不起作用,html,angular,angular7,Html,Angular,Angular7,每当我在HTML表中单击一行时,我都试图调用一个函数,但它不起作用,也没有给出任何错误 我尝试过几种方法,在线代码显示我的尝试方式应该是有效的 我的代码如下: Html: 当前,当我尝试调用onUpdate方法时,我的控制台没有打印日志。预期的结果是在日志中看到一些输出,但我不知道它为什么不触发onUpdate方法。在angular 7中,您可以使用(单击),这类似于angular JS的ng click 尝试: Userstories 新故事 {{story.id} {{story.name}
当前,当我尝试调用onUpdate方法时,我的控制台没有打印日志。预期的结果是在日志中看到一些输出,但我不知道它为什么不触发onUpdate方法。在angular 7中,您可以使用
(单击)
,这类似于angular JS的ng click
尝试:
Userstories
新故事
{{story.id}
{{story.name}
点击我!
ng click
实际上是用于AngularJS(1.x)
您想使用(单击)
,这是用于角度的
文档:事件被触发两次。在
和
中。
试试这个:
<h2>Userstories</h2>
<button (click)="onNewStory()">New Story</button>
<table>
<tr *ngFor="let story of userstories" [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>
Userstories
新故事
{{story.id}
{{story.name}
点击我!
用替换ng单击
(单击)
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);
}
<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>
<h2>Userstories</h2>
<button (click)="onNewStory()">New Story</button>
<table>
<tr *ngFor="let story of userstories" [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>