栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

提供给routeProvider的调试路由

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

提供给routeProvider的调试路由

您可以收听发出的多个事件

$routeservice
。这些事件是:

  • $routeChangeStart
  • $routeChangeSuccess
  • $routeChangeError
  • 和,
    $routeUpdate

(我鼓励阅读链接提供的文档,以获取每个文件的描述。)

此时,您可以在一个

$scope
控制器或指令之一上侦听一个或所有这些事件,或者通过注入
$rootScope
到您的
angular.module().run()
函数或另一个服务/工厂中。

例如,作为您提供的代码的附录:

reportFormsApp.config(function ($routeProvider) {  $routeProvider    .when("/Reporting", { templateUrl: "ReportForm/rfTemplate.html", controller: "rfController" })    .when("/Dashboard", { templateUrl: "DashboardForm/dfTemplate.html", controller: "dfController" })    .when("/Analysis",  { templateUrl: "AnalysisForm/afTemplate.html", controller: "afController" })    .otherwise({ redirectTo: "/Reporting" })});reportFormsApp.run([  '$rootScope',  function($rootScope) {    // see what's going on when the route tries to change    $rootScope.$on('$routeChangeStart', function(event, next, current) {      // next is an object that is the route that we are starting to go to      // current is an object that is the route where we are currently      var currentPath = current.originalPath;      var nextPath = next.originalPath;      console.log('Starting to leave %s to go to %s', currentPath, nextPath);    });  }]);

您还可以访问其余的

$routeProvider
对象,例如
current.templateUrl
next.controller

有关redirectTo的注意事项: 使用

$routeservice
事件有一个对象异常,那就是存在重定向时。在这种情况下,
next.originalPath
将是undefined,因为您所拥有的只是
redirectTo
您在中定义的属性
otherwise()
。您将永远不会看到用户尝试访问的路由。

因此,您可以收听或事件:

$locationservice's

$locationChangeStart``$locationChangeSuccess

reportFormsApp.run([  '$rootScope',  function($rootScope) {    // see what's going on when the route tries to change    $rootScope.$on('$locationChangeStart', function(event, newUrl, oldUrl) {      // both newUrl and oldUrl are strings      console.log('Starting to leave %s to go to %s', oldUrl, newUrl);    });  }]);

如果使用HTML5Mode,则

$locationChange*
事件将提供其他两个参数。这些是
newState
oldState

总之,侦听

$route*
事件可能是调试中提供的对象和定义的最佳选择
$routeProvider
。但是,如果您需要查看所有尝试的URL,
$locationChange*
则将需要侦听事件。

截至 AngularJS 1.3.9版本



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/426959.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号