1

in my Angular 1.x app I am getting a list of Offers via an api call to my backend.

For each Offer returned in the reply I am creating an ng-form. I then display the forms in a modal and I want to be able to disable each form's submit button when it has been clicked to avoid multiple clicks whilst the form data is posted to the back end.

This seems tricky since the number of Offers is an unknown, therefore I'm not sure how I can initialize a variable for each Offer in order to disable the button.

The task would be far more straight forward if I just had one single form, I colud set:

$scope.disableButton = true

... then use ng-disabled on the button

Thusfar I am displaying my forms as follows:

<div ng-form ng-repeat="i in offers track by $index" name="messageForm[$index]" class="row ng-cloak">

     ....
     <button type="button" ng-click="offerRespond(messageForm[$index])" ng-disabled="!messageForm[$index].$valid || offer.i.disableButtons">Submit</button>

</div>

Then in my controller's offerRespond function:

offer = this;
offer.i.disableButtons = true;

This doesn't work of course but it is as close as I can get.

A hack would be to parse the Offers object before passing it to the frontend but that just seems like a horrible hack.

1
  • Why not pass either the index or the object instance into offerRespond and track the state of the button there? It is hard to say exactly what you should write to disable/enable the button again but if you provide some code for offerRespond that would help. Commented Feb 20, 2019 at 20:40

1 Answer 1

1

Actually I almost had the answer in my implementation, I just was referring to my variables in correctly:

ng-disabled="!messageForm[$index].$valid || offer.i.disableButtons"

should have been

ng-disabled="!messageForm[$index].$valid || i.disableButtons"

Thanks to @igor for giving me an idea to test which enabled me to revealed the answer myself.

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

Comments

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.