-1

I am setting up a web form using angular whereby users will complete the form, get a response from the server, and then get redirected to another page.

angular.module('discountController', ['discServices'])

.controller('discCtrl', function(Discount,  $timeout, $location){
    var app = this;

    app.reqDiscount = function(discountData) {
            Discount.reqDiscount(app.discountData).then(function(data) {
                if (data.data.success) {
                    app.successMsg = data.data.message ; // Create Success Message
                    $timeout(function(){
                        console.log('timeout fired');
                        $location.path('/discountoutcome');                        
                        } , 3000);
                } else {
                    app.errorMsg = data.data.message; // Create an error message
                }
            });
        }; 
    });    

In the above code I call the app.reqDiscount function and I successfully get a response from the server which populates the app.successMsg variable. But the $timeout function does not then work. However, if I place the $timeout function outside of the if-else statement it does work. This is not what I want, because if there is an error I want to stay on the page.

In other areas of my code I have placed a $timeout function in a nested position like this and it works, so I don't understand why it would not work here.

Any help you can give me would be great. Even if you can not answer this question if you can give me some tips on debugging that would be helpful as console.log() is my only method of debugging currently. I am using Visual Studio Code.

Chrome console: Chrome console screen shot

Visual Studio Console: VSC console

4
  • As far as i can see the code looks fine. can you share a print screen of you're console. Commented Apr 3, 2019 at 8:01
  • @JonathanDsouza screenshots are attached. As you can see in the Chrome screenshot, the $timeout seems to stop after app.successMsg. Commented Apr 4, 2019 at 20:13
  • can you add the response you are getting in the promise of Discount.reqDiscount(app.discountData) ? Commented Apr 4, 2019 at 20:53
  • Forgive my lack of experience here but how do I do that? Commented Apr 6, 2019 at 9:34

1 Answer 1

0

I got it. I had coded the API incorrectly. data.data.success was returning false instead of true. Thanks for your help.

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.