【发布时间】:2017-06-22 21:55:32
【问题描述】:
我的 Angular JS 应用程序中有一个 $routeProvider,它不会在 ng-view 中加载模板。
我已经设置了<html data-ng-app="myApp"> 和<section data-ng-view></section。
它既不加载模板(甚至不生成 XHR),也不重定向到其他路径(如 /#/foo/bar/foo/ 并且不会抛出错误。
这是我的配置:
angular.module('myApp', ['myAppFilters'])
.config [
'$routeProvider',
($routeProvider) ->
$routeProvider
.when '/:year/:month',
templateUrl: 'partials/detail.html'
controller: DetailCntl
.when '/:user/:year/:month',
templateUrl: 'partials/detail.html'
controller: DetailCntl
.otherwise
redirectTo: '/'
]
编辑:这是编译好的 JS:
angular.module('myApp', ['myAppFilters']).config([
'$routeProvider', function($routeProvider) {
return $routeProvider.when('/:year/:month', {
templateUrl: 'partials/detail.html',
controller: DetailCntl
}).when('/:user/:year/:month', {
templateUrl: 'partials/detail.html',
controller: DetailCntl
}).otherwise({
redirectTo: '/'
});
}
]);
编辑#2:我自己找到了解决方案:
我的factories.coffee 中有这一行,它覆盖了配置:
angular.module('myApp', []).factory 'api', ($http, $q, $cacheFactory) ->
...
现在我已将配置分配给@myApp,并且正在使用@myApp.factory 'api', ...,它正在工作。
【问题讨论】:
-
配置有错字
[,应该是( -
@Chandermani 谢谢,但没关系。我添加了编译好的JS,上面的代码是CoffeeScript。
-
您是否在浏览器控制台中看到一些错误?
-
@Chandermani 不,正如我所写,它不会引发任何错误。
-
您是如何将脚本包含在您的 html 中的。这可能是脚本排序问题。
标签: javascript angularjs coffeescript url-routing