根据Http响应启用/禁用按钮

时间:2019-10-10 06:07:27

标签: .net ajax angular

我目前正在开发一个以.net为后端的Angular应用程序。我正在寻找有关如何在ajax发布请求成功或在后端(c#)上准备好响应时如何在Angular中启用按钮的指南。

appcomponent.html

 <button mat-button (click) = "cvalid()" >submit</button>
 <button mat-button id="getdoc" (click) = "getdoc()" [disabled] = "disabled" >Download</button>

appcomponent.ts

cvalid(){
 $.ajax({
    type: "POST", 
    url: 'http://localhost:5000/api/D/Postdata',       
    data: d1,
   contentType: "application/json",    
  success: function (data) {
    console.log(data)
    console.log(typeof(data));
    this.disabled = false ; //button trigger .
  }, 
  error: function (data) {
      console.log('error in sending data...:(');

 },
  });
}

控制器

  [HttpPost]
   public IActionResult Postdata([FromBody] RootObject data)
   {
        var data1 = JsonConvert.SerializeObject(data)
        return Ok(data1);

   }

请为此指导我。预先感谢。

1 个答案:

答案 0 :(得分:1)

声明that = this,以便您对此有参考

尝试这样:

cvalid(){
 this.disabled = true;
 var that = this;
 $.ajax({
    type: "POST", 
    url: 'http://localhost:5000/api/D/Postdata',       
    data: d1,
   contentType: "application/json",    
  success: function (data) {
    console.log(data)
    console.log(typeof(data));
    that.disabled = false ; //button trigger .
  }, 
  error: function (data) {
      console.log('error in sending data...:(');

 },
  });
}
  

注意:在Angular中,您应该使用HttpClient