ng click with Async method callback ? Pro please help

193 views
Skip to first unread message

Hengkeat Yam

unread,
Jul 17, 2015, 6:56:16 AM7/17/15
to ang...@googlegroups.com
<button ng-click="resource.$saveAsync(); form.isShow = false;">submit</button>

A simple question. $saveAsync is a simple ajax put request. I hope after response then my form will be closed.
Any idea ? 



Sander Elias

unread,
Jul 17, 2015, 10:25:58 PM7/17/15
to ang...@googlegroups.com
Hi Hengkeat,

What is it you want to show you user after he is done with the form?

Regards
Sander

Hengkeat Yam

unread,
Jul 17, 2015, 10:38:52 PM7/17/15
to ang...@googlegroups.com
 i want to close the form after ajax. 
 normally we write a click event on the controller 
 like 

 $scope.submitForm = function () { 
       $http({
           ...
       }).success(function () {
             $scope.showForm = false; //we use a success callback to close the form 
       })
  }

  but now, i hope can direct write this use angular expression. 
  something like  : 
  ng-click="resource.$saveAsync(); showForm = false;"
  is it possible ? 

Sander Elias

unread,
Jul 17, 2015, 11:03:38 PM7/17/15
to ang...@googlegroups.com
Hi Hengkeat,

Sure, that will work. But that is a bit too much logic into the template for my opinion. Why would you want do do that? The controller function will do just fine?

Regards
Sander

Hengkeat Yam

unread,
Jul 17, 2015, 11:18:32 PM7/17/15
to ang...@googlegroups.com
 no, that not working well , because i can't write an async method callback by angular expression. 
 ng-click="resource.$saveAsync(); showForm = false;" 
 the "showForm = false" will execute immediately, no waiting for the async callback.
 any idea ? 

 But that is a bit too much logic into the template for my opinion <--  For me It is still acceptable, just two action save and close. 
 Because i often change my template design and the event action. So i move some logic into template , let the view can be more flexible. 

Sander Elias

unread,
Jul 18, 2015, 12:36:52 AM7/18/15
to ang...@googlegroups.com
On Saturday, July 18, 2015 at 5:18:32 AM UTC+2, Hengkeat Yam wrote:
 no, that not working well , because i can't write an async method callback by angular expression. 
 ng-click="resource.$saveAsync(); showForm = false;" 
 the "showForm = false" will execute immediately, no waiting for the async callback.
 any idea ? 
Yes, do it in the controller :-P
If you make the showForm=false a function of the controller, you might be able to do the .then in the template. 


 But that is a bit too much logic into the template for my opinion <--  For me It is still acceptable, just two action save and close. 
 Because i often change my template design and the event action. So i move some logic into template , let the view can be more flexible. 
But it isn't 2 actions is it? you want to wait for a promise to clear too right? And what if the promise rejects? Sure, you can fork all of this over to the template, but maintaining such a thing will become more and more challenging.

Regards
Sander

Hengkeat Yam

unread,
Jul 18, 2015, 1:03:32 AM7/18/15
to ang...@googlegroups.com
It is all right. 
I have my structure design. I know when i should use controller or template to let maintain and changing more easier , this is a balance considerations.
whatever, for this question post, i just want to know does angular have a way to do this, maybe i miss on it.
Thank a lot for your guide, Sander Elias. 
Reply all
Reply to author
Forward
0 new messages