【发布时间】:2015-03-23 19:27:26
【问题描述】:
我正在尝试划分一个 Angular 应用程序并遇到问题让 ngView 正常工作。路由似乎配置正确,当我记录它的属性时,我得到:$route、$routeParams 和 $location,我得到:
Object {routes: Object, reload: function, updateParams: function}
Object {}
和
LocationHtml5Url {$$html5: true, $$protocol: "http", $$host: "localhost", $$port: 3000, $$parse: function…}
我读到here$routeParams 可能由于其异步性质而显得空,所以我认为这不是问题,但我不确定我错过了什么。
这是错误消息:
GET http://localhost:3000/partials/projectBlocks404(未找到)
我知道我应该相对于我的应用程序的根进行路由,我相信我是这样,所以我不确定它为什么要在看起来是 ../public/ 的地方寻找部分。 ../ 文件夹(app 是典型的 express 结构)
这是我的代码:
玉观(在./views中)
div(ng-controller='projects')
div(ng-view)
部分视图 (./views/partials)
p hey you found me!
控制器(在 /public/ 目录中)
angular.module('mean-man')
.controller('projects', ['$scope', '$http', '$route', '$routeParams', '$location', function($scope, $http, $route, $routeParams, $location){
console.log($route);
console.log($routeParams);
console.log($location);
this.$route = $route;
this.$location = $location;
this.$routeParams = $routeParams;
angular app.js 文件(在 /public/ 目录中)
(function(){
angular.module('mean-man', ['ngRoute','ngAnimate','mm.foundation'])
.config(['$routeProvider', '$locationProvider',
function($routeProvider, $locationProvider){
$routeProvider
.when('/:member', {
templateUrl: 'partials/projectBlocks',
controller: 'projects',
controllerAs: 'project'
});
$locationProvider.html5Mode({enabled:true,requireBase:false});
}]);
})();
我离开了 AngularJS 官方文档和 this 网站,所以我的代码可能是两者的混合,感谢任何帮助/参考!
【问题讨论】:
-
把html5mode的代码注释掉还能用吗?
-
它没有,实际上我添加了该行,因为我在另一个答案中找到了建议,尽管我现在找不到链接
-
哦,所以看起来该路径实际不存在,但它不存在。该文件在视图内,对吗?所以 templateUrl 应该是 'views/partials/projectBlocks.html'
-
是的,就是这样。我的应用程序结构如下:视图位于 ./views/partials 中:./views/partials 并且我的 Angular 应用程序位于:/public/javascripts/app。哦,但是我的文件是 projectBlocks.jade,它需要是 .html 扩展名吗?