本文介绍了AngularJS - 点火控制器时,获得previous路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 角的内部再次莫名其妙我。Angular's internals are baffling me again.我需要确定当一个特定的视图加载previous路线。I need to determine the previous route when a specific view is loaded.这是我如何做它app.controller('TrashCtrl',function($scope,$rootScope){ $rootScope.$on('$locationChangeSuccess',function(evt, absNewUrl, absOldUrl) { var hashIndex = absOldUrl.indexOf('#'); var oldRoute = absOldUrl.substr(hashIndex + 2); console.log(oldRoute); });});不幸的是我不得不打电话与该控制器对应一旦逻辑开始工作前视图。当控制器火灾,没有被记录的第一次。此外,这种初始加载后,应用程序将记录的每个 routeChange,即使装载的观点不符合 TrashCtrl Unfortunately I have to call the view that corresponds with that controller once before the logic starts to work. The first time when the controller fires, nothing is logged. Furthermore, after this initial load, the application will log every routeChange, even if the loaded view is not working with TrashCtrl我想它是这样的:在当前视图垃圾箱(其控制器是 TrashCtrl )加载,我需要previous路线或一个空字符串,如果这是第一个。如果当实际垃圾箱路由被称为这只火,而不是每个routechange这将是很好。When the view Trash (whose controller is TrashCtrl) is loaded, I need the previous route or an empty string if this was the first one.It would be nice if this only fires when the actual trash route is called, not on every routechange.如何将一个做到这一点?How would one achieve this?编辑:请注意,我知道的创建自定义的服务和writng每次更改它的可能性。这不是我想要的。我想只有fecth数据被需要时,例如就在回收站被调用。Note that I am aware of the possibility of creating a custom service and writng each change to it. This is not what I want. I would like to fecth the data only when it is needed, e.g just when Trash is called.推荐答案我试过曾经我可以的,但它似乎没有直接的办法让内获得了previous路由的路径在 $ routeProvider 的决心属性。I tried what ever I could, but it seems there is no straightforward way to get access to the previous route's path inside of the $routeProvider's resolve property.只是为了好玩,我实现了具有以下属性的(可笑复杂的)解决方案:Just for fun, I implemented a (ridiculously complex) solution with the following attributes:它可以检测只有在 /垃圾桶视图被加载(不是对每个路由)的previous路线。它不必知道路由的其他任何事情。增加额外的路由时不需要额外的配置(这增加了实现的复杂性,但简化修的)。It detects the previous route only if the /trash view is loaded (not for every route).It doesn't have to know anything about the rest of the routes.No extra configuration is needed when adding additional routes (this adds implementation complexity, but simplifies maintainance).下面是它如何工作的普通的话:Here is how it works in "plain" words:在决心对象,它注册为下一 $ routeChangeSuccess 事件侦听器。结果一旦事件被捕获,它注销监听器。结果一旦监听器是未注册的,它解决了与previous路由的路径的价值承诺。注入对象 trashCtrl (从解析属性)是一个函数,返回上述承诺。控制器,接收函数,调用它得到的承诺并等待承诺获得与previous路由的路径解决。In the resolve object, it registers a listener for the next $routeChangeSuccess event.Once the event is captured, it unregisters the listener.Once the listener is unregistered, it resolves a promise with value of the previous route's path.The object injected into the trashCtrl (from the resolve property) is a function, that returns the aforementioned promise.The controller, receives that function, calls it to get the promise and waits for the promise to get resolved with the previous route's path.简单的馅饼! 注意:对于简单的缘故,我已经没有考虑到导致 $ routeChangeError 错误(而不是 $ routeChangeSuccess )。一个或许应该考虑到这一点,并注销了听众,但即使你不这样做,不会有坏处 - 监听器将离开,直到 $ routeChangeSuccess 被激发一旦。 Note:For the sake of "simplicity" I haven't taken into account the posibility of an error leading to $routeChangeError (instead of $routeChangeSuccess). One should probably take that into account and deregister the listeners, but even if you don't, there will be no harm - the listener will leave on until the $routeChangeSuccess is fired once.控制器:app.controller('trashCtrl', function ($scope, prevRoutePromiseGetter) { prevRoutePromiseGetter().then(function (prevRoute) { $scope.prevRoute = prevRoute || 'nowhere'; });});的解析对象:resolve: { prevRoutePromiseGetter: function ($q, $rootScope) { var deferred = $q.defer(); var dereg = $rootScope.$on('$routeChangeSuccess', function(evt, next, prev) { dereg(); deferred.resolve((prev.originalPath || '').substr(1)); } ); return function () { return deferred.promise; }; }}又见这个 短演示 See, also, this short demo. 这篇关于AngularJS - 点火控制器时,获得previous路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-23 17:48