$timeout 函数在 if 语句中不起作用
$timeout function not working inside if statement
我正在使用 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.successMsg 变量。但是 $timeout 函数不工作。但是,如果我将 $timeout 函数放在 if-else 语句之外,它就可以工作。这不是我想要的,因为如果出现错误我想留在页面上。
在我的代码的其他区域,我在这样的嵌套位置放置了一个 $timeout 函数并且它可以工作,所以我不明白为什么它在这里不起作用。
如果你能给我任何帮助,那就太好了。即使你不能回答这个问题,如果你能给我一些调试技巧,那会很有帮助,因为 console.log() 是我目前唯一的调试方法。我正在使用 Visual Studio 代码。
Chrome 控制台:
Chrome console screen shot
Visual Studio 控制台:
VSC console
我明白了。我对 API 的编码有误。 data.data.success 返回的是假而不是真。感谢您的帮助。
我正在使用 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.successMsg 变量。但是 $timeout 函数不工作。但是,如果我将 $timeout 函数放在 if-else 语句之外,它就可以工作。这不是我想要的,因为如果出现错误我想留在页面上。
在我的代码的其他区域,我在这样的嵌套位置放置了一个 $timeout 函数并且它可以工作,所以我不明白为什么它在这里不起作用。
如果你能给我任何帮助,那就太好了。即使你不能回答这个问题,如果你能给我一些调试技巧,那会很有帮助,因为 console.log() 是我目前唯一的调试方法。我正在使用 Visual Studio 代码。
Chrome 控制台: Chrome console screen shot
Visual Studio 控制台: VSC console
我明白了。我对 API 的编码有误。 data.data.success 返回的是假而不是真。感谢您的帮助。