如何查找所有加载的路线?

时间:2015-02-02 19:33:46

标签: angularjs angular-ui-router

我有这样的功能,应该加载一些路线。

.config(function($stateProvider, $urlRouterProvider){

    $stateProvider.state('app', {
      abstract: true,
      templateUrl: "index.html",
    })

  $stateProvider.state('index', {
    url: '/',
    views: { "index" : { templateUrl: "index.html" } },
    parent: "app",
  });

    $stateProvider.state('register', {
    url: "/register",
    views: { "register" : { templateUrl: "templates/register.html", } },
    parent: "app",
  });


  $urlRouterProvider.otherwise("/");
}) 

但我得到了

  

错误:无法从状态''

解析'register'

异常。从我检查的所有其他示例中,该路由配置看起来很好。但这不是某种原因。

我怀疑我的路由未加载,我将console.logdebuggeralert函数放在我的.config函数中,但都没有执行。

有没有办法从ui-router列出所有加载的路由?

1 个答案:

答案 0 :(得分:0)

  

免责声明:我想给你一个提示。如果这个答案不适合,请告诉我,将删除。很明显,我以前的回答不适合你:Could not resolve '...' from state ''

可能是这样的错误消息的来源是什么?

  

无法解决州''

中的'注册'

只有两种选择。

  • 未加载具有状态定义的文件。 (可以使用某些console.log内容查看)
  • 文件代表module已正确加载,但未被根ng-app="myApp"
  • 引用

我强烈期望,第二个案例是问题。这是一个BROKEN example,它会(按钮点击后)显示错误:Could not resolve 'register' from state ''

发生了什么事。有一个名为“otherStates.js”的文件,它被加载到 index.html

...                      //查看上面加载的脚本    //下面是根模块名称“myApp”        ...

“otherStates.js”声明模块“myStates”,其中包含所有状态:

// will always be in a console
// because file is loaded
console.log("file is accessed");

angular
 .module('myStates', ['ionic'])
 .config(function($stateProvider, $urlRouterProvider){

    // never reach console, because this module is not used
    console.log("module myStates is not referenced in myApp ");
    console.log("these lines will never be trigerred");

  $stateProvider

  .state('app', {
      abstract: true,
      ...
    })
  ....

所以为什么我们确实收到了这个错误?如何解决?

简单地说,在script.js(我们的根模块在哪里),我们有这样的声明:

var app = angular.module('myApp', ['ionic'])

这还不够。我们还需要引用myStates。所以我们必须像这样改变它,一切都会起作用

var app = angular.module('myApp', ['ionic', 'myStates'])

选中此broken example