视图之间传递对象(Flash消息) [英] Passing object between views (flash message)
问题描述
这是在下面的方案来传递信息的最好方式。
在 $ scope.p的成功场景。$保存
,结果包含消息( res.message
),我想在未来的视图中显示( $ location.path(/测试/+ res.reply.Id)
)。没有棱角,我可以通过它的URL或将其保存在会话cookie。但是,我想因为没有浏览器重定向,国家应提供可能会有Angualr一个更好的办法。什么是实现这一目标的最佳途径?
在rootScope设置它显示了它,而我使用的浏览器后退按钮,以及消息的范围应当只在第导航到新视图。
函数NewCtrl(电话,$位置$ rootScope,$范围){
$ scope.p =新手机();
$ scope.save =功能(){
$ scope.p。$保存(
{},
功能(RES){
$ rootScope.message = res.message ** //< - 这将导致浏览器使用时后退按钮等消息仍然设置**
$ location.path(/测试/+ res.reply.Id); ** //< - 所需物品:需要将信息传递给下一个视图**
},功能(RES){
//去做
}
);
};
}
....
PhoneApp.factory('电话',函数($资源){
返回$资源('/ API /测试/:_ ID)
});
您可以使用它显示在$ routeChangeSuccess闪光灯的服务。
每个设置闪光邮件时,将其添加到队列中,当路由变化采取的第一个项目从队列并将其设置为当前消息。
下面是一个演示:
http://plnkr.co/edit/3n8m1X?p=$p$pview
What is the best way to pass message in the below scenario.
In the success scenario of $scope.p.$save
, the result contains a message (res.message
), which I like to display in the next view ($location.path("/test/"+res.reply.Id)
). Without Angular, I may pass it in the url or save it in session cookies. But, I guess there might be a better way in Angualr as there is no browser redirect and the state should be available. What is the best way to achieve this?
Setting it in rootScope shows it while I use browser back button, and the scope of the message should only for the first navigation to the new view.
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')
});
You could use a service which displays the flash on $routeChangeSuccess.
Each time you set a flash message, add it to a queue, and when the route changes take the first item off the queue and set it to the current message.
Here's a demo:
http://plnkr.co/edit/3n8m1X?p=preview
这篇关于视图之间传递对象(Flash消息)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!