【问题标题】:$routeProvider.when(..) not catching all URLs$routeProvider.when(..) 没有捕获所有 URL
【发布时间】:2014-07-24 22:52:16
【问题描述】:

设置:jade、angular、express、node。

在我的 routeProvider 角度中,我遇到了一些问题。我正在请求一个带有未捕获参数的 url。

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

     app.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
      $routeProvider
         //... omitted some url/pages here
         //start of SPA
         .when('/dashboard/', {
            templateUrl: 'partials/index',
            controller: 'dashIndexCtrl'
        })
        .when('/dashboard/class/:id',{
            templateUrl: 'partials/class',
            controller: 'classCtrl'
        })
        .otherwise({
            redirectTo: '/'
        }); 

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

当我打电话给localhost/dashboard/class/TEST

我被重定向到根目录。

我在服务器端声明我的路由如下:

app.get('/dashboard', routes.dashboard);
app.get('/dashboard/partials/:name', routes.partials);
app.get('/api/class/:id', api.classGet);

我的控制器名称与routeProvider 匹配。在这种情况下,控制器代码如下:

app.controller('classCtrl', function($scope, $http, $routeParams){
    $http.get('/api/class/' + $routeParams.id)
        .success(function(dataJson){
            console.log(dataJson);
        });
});

为什么routeProvider 缺少此网址?我在我的控制器中设置了断点,所以我确定它没有被调用。我的partials/class.jade 也没有被调用。如果您需要更多代码或目录信息,请告诉我。提前致谢。


经过进一步调查:
如果我去:localhost/dashboard/partials/class 它会加载那个玉文件。所以它肯定是控制器。

目录结构,我相信这是我现在的问题....

+---\
|   +---\views
    |   +---index.jade
    |   +---dashboard.jade
    |   +---\partials
        |   +---index.jade
        |   +---class.jade

【问题讨论】:

  • 如果你访问地址localhost/partials/class浏览器返回什么?解析模板的代码?
  • @KrzysztofSafjanowski 它只会将我重定向到/ 根文件夹。即使我拿出otherwise()

标签: javascript node.js angularjs route-provider


【解决方案1】:

查看控制台并观察 AngularJS 尝试从 /partials/index 然后从 /dashboard/partials/index 加载模板

解决方案

在配置部分将 templateUrl 路径设置为/dashboard/partials/index

原因:angular-route.js

templateUrl = $sce.getTrustedResourceUrl(templateUrl);

if (angular.isDefined(templateUrl)) {
    next.loadedTemplateUrl = templateUrl;
    template = $http.get(templateUrl, {cache: $templateCache}).
        then(function(response) { return response.data; });
}

templateUrlroute path 没有串联 - 只需从应用程序的 Angular 根目录加载 tempalteUrl

【讨论】:

  • 我理解这样做的原因并做出了改变。但是,即使使用/dashboard/partials/class/dashboard/partials/index,它仍然会重定向我,特别是在尝试访问路由localhost/dashboard/indexlocalhost/dashboard/class 时。但是localhost/dashboard/ 将加载索引文件...
  • /dashboard/ 应该加载模板/dashboard/partials/index/dashboard/class/whoteverOrEmpty 应该加载模板/dashboard/partials/class。是什么在重定向你——AngularJS 还是 Express?
  • 角。它经过.when('/dashboard/class/:id'.... 并重定向到.otherwise({redirectTo: '/'。查看我的服务器控制台时,我可以看到对 /dashboard/classGET 请求,这不应该是,我应该看到 /api/class/ 的请求,这是控制器中的 GET。再次感谢。
  • 浏览器可以加载解析过的jade模板吗?
  • 尝试从浏览器加载/api/class/id,其中 id 是您的应用程序可以处理的内容。有效吗?
猜你喜欢
  • 2013-07-12
  • 2021-01-27
  • 2011-06-11
  • 1970-01-01
  • 2011-09-27
  • 1970-01-01
  • 2019-05-13
  • 2013-09-09
  • 2019-10-20
相关资源
最近更新 更多