1

I am currently developing an Angular App with .net as Backend. I am looking for guidance on how to Enable a button in Angular, when an ajax post request is successful or when the response is ready on backend(c#).

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...:(');

 },
  });
}

controller

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

   }

Please guide me regarding this. Thanks in Advance.

1
  • I doubt if your disabled is being set, can you see the value of data in console? If you use arrow function then probably it will be set correctly. And why are you using jquery in angular, you should use httpClient provided by angular. Commented Oct 10, 2019 at 9:15

1 Answer 1

1

Declare that = this so you have a reference of this

Try like 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...:(');

 },
  });
}

Note: In Angular, you should use HttpClient

Sign up to request clarification or add additional context in comments.

1 Comment

This will work but as suggested please consider using HttpClient.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.