我有一个表单,在提交时调用以下函数。
function addToCart(form) {
if (form.$valid) {
shopFactory.addToCart(vm.model.ProductDetail).then(function () {
toastr.success("Added to cart");
$window.location = "shop/myCart";
});
}
}
该函数重定向到页面 myCart,并显示一条 Toastr 消息。
我的问题是,当用户被引导离开的当前页面上出现该消息时,toastr 消息会立即消失。
有没有办法让消息通过重定向持续存在,或者只是出现在下一页上?
最佳答案
在shop/myCart页面 Controller 内,在里面编写代码,$stateChangeSucces
(如果使用ui.router)或$routeChangeSuccess
。还要检查之前的路由以enusre该toastr仅在这种情况下才会显示消息。
示例:
$scope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams){
console.log("foo");
});
});
关于javascript - 重定向页面时显示 Toastr 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35123934/