pagemethod 有效,但是当我使用 webmethod 时,它会刷新页面,从而破坏了首先使用 angular 2 的原因。
如何防止表单刷新页面?
索引.aspx
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManagerMain"
runat="server"
EnablePageMethods="true" >
</asp:ScriptManager>
<my-app>Loading...</my-app>
</form>
</body>
index.aspx.cs
[WebMethod]
public static string getString()
{
return "Test";
}
app.component.html
<div>
<Button (click)="btnSubmit_Click">test</Button>
</div>
应用程序组件.ts
btnSubmit_Click()
{
var test = window['PageMethods'].getString(this.onSucces, this.onError);
}
注意:
目前,我正在尝试为公司使用 angular 2 和 aspx,如果它可行,它可能会成为小型前端 api 的标准,并且由于它的信息非常有限,我感谢任何帮助。
最佳答案
您应该将按钮 type
设置为 button
,因为默认情况下按钮 type
是 submit
。类型为 submit
的 button
可能会导致回发页面。
<div>
<Button type="button" (click)="btnSubmit_Click()">test</Button>
</div>
关于asp.net - 如何在 aspx 中以 Angular 2 停止页面刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40220207/