AngularJS routeProvider从数组生成路由

时间:2016-01-14 11:13:41

标签: javascript arrays angularjs routing routes

我试图为$routeProvider获取路由解析,以便从数组生成.when()' s。这是我尝试过的app.js中的代码。这会生成正确的路由,但会导致无限循环,并且尝试多次加载AngularJS"。我只在索引中加载angularjs。 html文件,视图呈现在其中。如何从阵列中获取路由?数组结构为"route":"fileName.html"

var dynape = angular.module("dynape",['ngRoute','dynape.controllers','dynape.services','ngCookies']);

dynape.config(['$routeProvider','$locationProvider',
function($routeProvider,$locationProvider) {
    var db = new PouchDB('http://localhost:5984/siteconf', {skipSetup: true});

    db.get("pages").then(function(doc) {
        var tmp = doc;
        delete tmp["_id"];
        delete tmp["_rev"];
        delete tmp["/"];
        for(p in tmp) {
            console.log(p.toString());
            $routeProvider.when(p.toString(), {
                controller: "SiteController",
                templateUrl: "views/pages/"+tmp[p]
            });
        }
    });
    // Follwing routes never change
    $routeProvider.when("/",{
        controller: 'SiteController',
        templateUrl: "views/frontPage.html"
    }).when("/admin",{
        controller: 'AdminLoginController',
        templateUrl: "views/admin/login.html"
    }).when("/admin/setup", {
        controller: 'SetupController',
        templateUrl: "views/admin/setup.html"
    }).when("/admin/dashboard", {
        controller: 'AdminActionController',
        templateUrl: "views/admin/dashboard.html"
    }).when("/admin/pages", {
        controller: 'AdminActionController',
        templateUrl: "views/admin/pages.html"
    }).otherwise({
        redirectTo: "/"
    });




    $locationProvider.html5Mode(true);
}
]);

angular.module("dynape.controllers",[]);
angular.module("dynape.services",[]);

我在其中呈现视图的index.html:

<!DOCTYPE html>
<html ng-app="dynape">
<head>
    <base href="/">
    <meta charset="utf-8">
    <title>A Dynape Site</title>
    <link rel="stylesheet" href="/css/base.css" media="screen">
    <link rel="stylesheet" href="/css/components-base.css" media="screen">
    <link rel="stylesheet" href="/css/gridsys.css" media="screen">

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">

</head>
<body>
    <div class="wrap" ng-view>
    </div>
    <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script>
    <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/md5.js"></script>

    <script type="text/javascript" src="src/vendor/jquery.js"></script>
    <script type="text/javascript" src="src/vendor/pouchdb.min.js"></script>

    <script type="text/javascript" src="src/vendor/image-picker.min.js"> </script>
    <script type="text/javascript" src="src/vendor/angular.min.js"></script>
    <script type="text/javascript" src="src/vendor/angular.route.min.js"></script>

    <script type="text/javascript" src="src/vendor/angular-cookies.js"></script>
    <script type="text/javascript" src="src/app.js"></script>
    <script type="text/javascript" src="src/services/AuthenticationService.js"></script>

    <script type="text/javascript" src="src/controllers/AdminLoginController.js"></script>
    <script type="text/javascript" src="src/controllers/AdminActionController.js"></script>
    <script type="text/javascript" src="src/controllers/SetupController.js"></script>
    <script type="text/javascript" src="src/controllers/SiteController.js"></script>


</body>
</html>

1 个答案:

答案 0 :(得分:0)

您无法在.config

之外访问$ routeProvider

我的猜测是db.get是对运行异步的服务器的调用。然后,当呼叫返回时,routeProvider已经注册。

换句话说,您的代码按以下顺序执行:

  1. 创建应用(groovy)
  2. 使用$ routeProvider(swell)
  3. 调用CONFIG
  4. 调用db.get(&lt; callback&gt;)(罚款)
  5. 手动设置$ routeProvider.when,有6条特定路线(真棒)
  6. ....现在,angularJs已被自助...

    1. db.get从服务器返回并调用&lt; callback&gt; (UH-OH)
    2. 尝试使用新的“动态”路线(oops)访问/更改$ routeProvider
    3. 2潜在解决方案:

      一个。 bootstrap / config angularJs db.get()返回后。从来没有试过这个...我想它可行,但似乎有风险。

      B中。使用装饰器的DEFER路线配置。关于这个主题的there is an excellent blog post似乎可以做你想做的事情。

      这意味着你所有的动态路线都需要相同的“根”(例如 - 它们都遵循“/ other /:route *”格式),但这是唯一的限制。

      我选择选项B.看起来像个好计划!