[英]jade template won't render with ui-router dynamic param
I'm using express 4 to render jade templates for angular. 我正在使用Express 4渲染角度的玉模板。 When I try and route with a dynamic parameter on the client, my routes don't match so express sends
layout.jade
twice. 当我尝试在客户端上使用动态参数路由时,我的路由不匹配,因此express两次发送
layout.jade
。
app.config(function($stateProvider, $urlRouterProvider, $locationProvider){
$stateProvider
.state('home', {
url: '/',
templateUrl: 'views/home',
controller: 'MainCtrl'
})
.state('about', {
// works fine
url: '/about',
templateUrl: 'views/about'
})
.state('post', {
url: '/post/{id}',
// view does not render
templateUrl: 'views/post',
controller: 'PostCtrl'
});
$urlRouterProvider.otherwise('/');
$locationProvider.html5Mode(true);
})
For when I hit http://localhost:3000/post/0
the request goes out to Request URL:http://localhost:3000/post/views/post
which does not match anything. 当我点击
http://localhost:3000/post/0
,请求转到Request URL:http://localhost:3000/post/views/post
,该Request URL:http://localhost:3000/post/views/post
不匹配任何内容。
I want to render public/views/post.jade
when hitting /post/{id}
我想在打
/post/{id}
时渲染public/views/post.jade
These is the express 4 routes and config: 这些是明确的4条路线和配置:
app.set('views', path.join(config.rootPath, '/views'));
app.set('view engine', 'jade')
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(config.rootPath, 'public')));
app.get('/views/:name', function(req, res){
res.render('../public/views/' + req.params.name);
})
app.get('*', function(req, res){
res.render('layout');
});
Note If I change the post state to use template: 注意如果我更改发布状态以使用模板:
.state('post', {
url: '/post/{id}',
template: '<div>{{ post }}</div> is that',
controller: 'PostCtrl'
});
The template will render fine. 该模板将很好地呈现。 The problem is when I use
templateUrl: 'views/post'
问题是当我使用
templateUrl: 'views/post'
Update: 更新:
From the home view I can navigate to see post.jade
with: 在主视图中,我可以浏览以下内容以查看
post.jade
:
div(ng-repeat='post in posts track by $index') a(ui-sref='post({ id:{{ $index }} })') Comments
That navigates to views/post
alright, but if I refresh the page I get the same layout-layout error 导航到
views/post
好了,但是如果刷新页面,则会出现相同的布局布局错误
(I'm assuming config.rootPath
is the root directory of your project. You should check out http://nodejs.org/docs/latest/api/globals.html#globals_dirname ) (我假设
config.rootPath
是项目的根目录。您应该检出http://nodejs.org/docs/latest/api/globals.html#globals_dirname )
So first of all, if you have all of your views in a directory {config.rootPath}/public/views
, this line: 因此,首先,如果所有视图都在目录
{config.rootPath}/public/views
,则此行:
app.set('views', path.join(config.rootPath, '/views'));
is basically telling Express to look for your jade files in {config.rootPath}/views
, which isn't the correct location. 基本上是告诉Express在
{config.rootPath}/views
查找您的玉器文件,这不是正确的位置。 Try: 尝试:
app.set('views', path.join(config.rootPath, 'public', 'views'));
This will allow you to just specify paths relative to public/views
whenever you want to render a jade file - so on the angular side of things, if you wanted to render public/views/post.jade
when hitting /post/{id}
, it would just be: 这样,只要您想渲染一个Jade文件,就可以只指定相对于
public/views
路径-因此,如果您想在public/views/post.jade
/post/{id}
时渲染public/views/post.jade
,则可以在事物的角度方面,那就是:
.state('post', {
url: '/post/{id}',
templateUrl: '/post',
controller: 'PostCtrl'
})
and on the backend, loading the views would just look like 在后端,加载视图就像
app.get('/:name', function(req, res){
res.render(req.params.name);
})
(Note: It's not the best idea to have a param as the base of the path. You'd want to have a directory public/views/partials
for your partials, so instead of '/:name'
- which would intercept pretty much everything - you'd use '/partials/:name'
) (注意:以param作为路径的基础并不是最好的主意。您希望为您的局部文件创建一个目录
public/views/partials
partials,因此要使用'/:name'
代替-这样会截取很多内容一切-您将使用'/partials/:name'
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.