简体   繁体   English

带有按钮单击事件的奇怪Angular 2行为

[英]Strange Angular 2 behavior with button click event

I have two very simple methods in my .ts file: 我的.ts文件中有两个非常简单的方法:

editLocation(index) {}

deleteLocation(index) { 
    this.locations.splice(index, 1); 
}

html counterpart calls these method when buttons are clicked: 当单击按钮时,html对应对象将调用以下方法:

<button (click)="editLocation(i)" class="btn btn-xs btn-success">Edit</button>
<button (click)="deleteLocation(i)" class="btn btn-xs btn-danger">Delete</button>

"Delete" button works fine, quickly deletes what I need and shows updated list of "locations". “删除”按钮可以正常工作,快速删除我需要的内容并显示“位置”的更新列表。 Now, with "Edit" button I wanted to implement a different behavior. 现在,使用“编辑”按钮,我想实现其他行为。 But, whatever I put in this method, after the code is executed, it thinks for a bit and redirects to a previous page. 但是,无论我在该方法中放入什么内容,在执行代码后,它都会花一点时间并重定向到上一页。 I deleted everything from the edit method (it is now empty as you can see above) and it still redirects to a previous page. 我从edit方法中删除了所有内容(如上所示,现在为空),它仍然重定向到上一页。 However, if I add this.locations.splice(index, 1); 但是,如果我添加this.locations.splice(index, 1); to the "edit" method it behaves just like "delete" method and does not redirect anywhere. 到“编辑”方法,它的行为就像“删除”方法一样,并且不会重定向到任何地方。 What can be wrong? 有什么事吗 Thanks a lot in advance! 在此先多谢!

您可以向其中添加type =“ button”,以确保该按钮不充当提交或其他事件:

<button type="button" (click)="editLocation(i)" class="btn btn-xs btn-success">Edit</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM