Button正在重新加载我的页面,即使他不是提交者 [英] Button is reloading my page even if he's not a submit

查看:78
本文介绍了Button正在重新加载我的页面,即使他不是提交者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Alpha:44

我目前对angular2中的按钮有问题...

I have a problem currently with the button in angular2...

它们似乎具有angular1中不存在的奇怪行为,甚至在任何纯HTML中也没有奇怪的行为

they seems to have strange behaviour that doesn't exist in angular1 and even in any pure html it doesn't do the strange behaviour

每次我单击我的按钮时,页面正在重新加载……这不是提交按钮..因此它不应该重新加载页面!

everytime i click on my button, the page is reloading... it's not a submit button.. so it shouldn't reload the page!

angular2中令人沮丧的另一种行为是很多错误导致浏览器重新加载,并且我们丢失了控制台日志...

another behaviour that is pretty frustrating in angular2 that a lot of error make the browser to reload and we lose the console log...

这是代码

createPlayer() {
    let p = new PlayerModel('s', 1);
    console.log(p);
}

<form>
    <div class="form-group">
        <label for="name" class="control-label">
            Name:
        </label>

        <input type="text" id="name" class="form-control"/>
    </div>

    <div class="form-group">
        <label for="score" class="control-label">
            Score:
        </label>

        <input type="number" id="score" class="form-control"/>
    </div>

    <div class="form-group">
        <button (click)="createPlayer()" class="btn btn-default">
            Create a Player
        </button>
    </div>
</form>

当我在第一条语句中使用调试器进行处理时,没有错误,但是当按钮结束时,它会重新加载页面...

there is no error while i do the process with debugger in first statement but when the end of the button happen it reload the page...

推荐答案

除非另行指定,否则提交表单是<button> s的 default 行为(我知道,对吗?).通过不包含form,添加type="button"或防止createPlayer中的默认行为,可以避免这种情况.

Unless you specify otherwise, submitting a form is the default behavior of <button>s (I know, right?). You can avoid this by not having a containing form, by adding type="button", or by preventing the default behavior in createPlayer.

这篇关于Button正在重新加载我的页面,即使他不是提交者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆