0

I have a button that both fires a function and redirects. Is it possible to stall the redirect until the function returns?

The reason I want to do this is because I have a second function on the destination page that uses variables set by the first function as parameters, right now the redirect happens before the first function has time to set the variables used as parameters in the second function.

My button

<button class="btn btn-sm btn-primary" ng-click="selectCharacter(character)" ui-sref="apply" ng-show="application">
        Select
</button>

html-tag calling second function with ng-init, using parameters set by the first function

<form ng-Submit="getCharacterInfo(apply)" ng-init="getCharInfo(current_user, current_user_realm)">
2
  • Why call 2 separate functions? At least you could have the button click do both and then redirect from there. I never use ng-submit Commented Dec 20, 2016 at 20:49
  • either you use one function, or use promises... or a combination of both. You should show some code Commented Dec 20, 2016 at 20:52

1 Answer 1

1

You could try removing the ui-sref attribute from the button, so it doesn't "redirect" automatically. When the selectCharacter function is done executing what it executes now, it can programmatically change states:

First, remove ui-sref:

<button class="btn btn-sm btn-primary" ng-click="selectCharacter(character)" ng-show="application">Select</button>

Then, update controller to programmatically redirect:

$scope.selectCharacter = function(char) {
    // do whatever needs to be done here

    // redirect to the 'apply' state programmatically. Don't forget to inject the $state service
    $state.go('apply');
};
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.