我将尝试将Express与Angular结合起来,特别是使用Angular-UI Router并使用Jade:
我的index.jade文件如下:
我的问题不是无法查看“关于”页面,而是无法查看主页。 我也尝试将index.jade中的div(ui-view)替换为
我的index.jade文件如下:
doctype html
html(lang="en")
head
meta(charset='UTF-8')
meta(name='fragment', content='!')
base(href='/')
title Node Express Angular Example
meta(name='viewport', content='width=device-width, initial-scale=1.0')
link(rel='stylesheet', href='//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css')
link(rel='stylesheet', href='./css/style.css')
body(style='', ng-app='myApp', ng-cloak)
include partials/header
div(ui-view)
script(src='//code.jquery.com/jquery-2.1.1.min.js')
script(src='//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js')
script(src='https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js')
script(src='http://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.10/angular-ui-router.min.js')
script(src='https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular-sanitize.js')
script(src='./js/app.js')
我的app.js看起来像这样:
var myApp = angular.module('myApp', ['ui.router']);
myApp.config(['$stateProvider','$urlRouterProvider','$locationProvider',
function ($stateProvider,$urlRouterProvider,$locationProvider) {
$stateProvider
.state('home', {
url:'/home',
templateUrl: './partials/partial-home.jade'
})
.state('about', {
url:'/about',
template: 'This is the about page'
});
$urlRouterProvider.otherwise('/home');
$locationProvider
.html5Mode(true)
.hashPrefix('!');
}]);
我的partial-home.jade文件如下:
div.jumbotron
div.container.text-center
h1 Home Page
p This page is a draft in progress
我的问题不是无法查看“关于”页面,而是无法查看主页。 我也尝试将index.jade中的div(ui-view)替换为
<div ui-view></div>
,正如其他SO帖子中建议的那样,但没有效果。有什么线索吗?
编辑:app.js中有一个拼写错误,应该是templateUrl而不是templateURL。然而,它仍然无法渲染正确的partial-home.jade,ui-view中显示的实际上与index.jade相同。我的server.js看起来像这样:var express = require('express');
var app = express();
var port = process.env.PORT || 8080;
var favicon = require('serve-favicon');
var path = require('path');
app.set('views', __dirname+'/client/views');
app.set('view engine', 'jade');
app.use(express.static(path.join(__dirname, 'client')));
app.get("/*", function (req, res) {
console.log(req.url + req.method);
res.render('index');
});
app.listen(port, function () {
console.log('Express Server listening on ' + port);
});
./partials/partial-home.jade
模板加载时的响应。它是真正的渲染后的 HTML 还是仍然是原始的 Jade 语法? - Peter Lyons