在以下场景中传递消息的最佳方式是什么?在视图之间传递对象(flash message)
在成功方案$scope.p.$save
中,结果中包含一条消息(res.message
),我希望在下一个视图($location.path("/test/"+res.reply.Id)
)中显示该消息。如果没有AngularJS,我可能会将它传递给url或将它保存在会话cookie中。但是,我想在AngularJS中可能会有更好的方式,因为没有浏览器重定向并且状态应该可用。达到此目的的最佳方法是什么?
设置它在rootScope中显示它,而我使用浏览器后退按钮,并且消息的范围应该只适用于第一次导航到新视图。
function NewCtrl(Phone, $location, $rootScope, $scope) {
$scope.p = new Phone();
$scope.save = function() {
$scope.p.$save(
{},
function (res) {
$rootScope.message = res.message **//<-- this will cause message still set when using browser back button, etc**
$location.path("/test/"+res.reply.Id); **//<-- Req: needs to pass the message to next view**
}, function (res) {
//TODO
}
);
};
}
....
PhoneApp.factory('Phone', function ($resource) {
return $resource('/api/test/:_id')
});
非常感谢你的帮助和努力。 – bsr
我刚刚编辑它,我想到了一个**更好更简单的实现,它使用数组支持多个闪烁。 –
您也可以考虑使用'$ routeChangeStart',因为有时(取决于您的设置)成功在进程中调用得太晚(新页面可能已经加载并且解决了警报范围)。 –