Angularjs $timeout函数在if语句中不工作

Angularjs $timeout函数在if语句中不工作,angularjs,angularjs-directive,angularjs-scope,Angularjs,Angularjs Directive,Angularjs Scope,我正在使用angular设置一个web表单,用户将完成表单,从服务器获得响应,然后重定向到另一个页面 angular.module('discountController', ['discServices']) .controller('discCtrl', function(Discount, $timeout, $location){ var app = this; app.reqDiscount = function(discountData) {

我正在使用angular设置一个web表单,用户将完成表单,从服务器获得响应,然后重定向到另一个页面

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
                }
            });
        }; 
    });    
在上面的代码中,我调用app.reqDiscount函数,并成功地从填充app.successsg变量的服务器获得响应。但是$timeout函数就不起作用了。但是,如果我将$timeout函数放在if-else语句之外,它就可以工作了。这不是我想要的,因为如果有错误,我想留在页面上

在我的代码的其他区域中,我将$timeout函数放置在这样的嵌套位置,它可以工作,所以我不明白为什么它在这里不能工作

你能给我的任何帮助都会很好。即使你不能回答这个问题,如果你能给我一些调试技巧,这会很有帮助,因为console.log()是我目前唯一的调试方法。我正在使用VisualStudio代码

Chrome控制台:

Visual Studio控制台:
我明白了。我的API编码不正确。data.data.success返回的是false而不是true。谢谢您的帮助。

据我所知,代码看起来不错。你能分享你的控制台的打印屏幕吗?@JonathanDsouza屏幕截图附在后。正如你在Chrome屏幕截图中所看到的,$timeout似乎在app.successsg之后停止。你能在
Discount.reqDiscount(app.discountData)
的承诺中添加你得到的响应吗?请原谅我在这里缺乏经验,但我该怎么做?