asp.net - 如何在 aspx 中以 Angular 2 停止页面刷新

标签 asp.net angular pagemethods

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,因为默认情况下按钮 typesubmit。类型为 submitbutton 可能会导致回发页面。

<div>
    <Button type="button" (click)="btnSubmit_Click()">test</Button>
</div>

关于asp.net - 如何在 aspx 中以 Angular 2 停止页面刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40220207/

相关文章:

c# - 使用 asp.net 和 c# 验证服务器端

c# - 无法加载DLL 'fbembed'

c# - 将参数发送到另一个 ASP.Net 页面

angularjs - 学完Angular 2+,学AngularJS到底有多难?

c# - asp.net中页面方法的返回值

asp.net - 防止用户在同一行上工作

angular 2 从父组件类访问子组件对象

javascript - 将div分成2列

asp.net - 上载大文件

Javascript 调用 vb.net 中的 pageMethod 不起作用