您需要进行一些更改。首先,正如Srivastva所说,您必须调用路由名称而不是模板。
路由文件:
.when('/reg', {
templateUrl: 'views/registration.html'
})
当html5mode设置为true时,你的href标签应该像这样:
<a href="reg">Register</a>
此外,为了使其正常工作,您需要在文档的头部添加以下内容:
<head>
<base href="/" />
</head>
设置html5mode为false后,你的href标签应该像这样:
<a href="#/reg">Register</a>
为了在服务器上处理html5,我使用了这个yeoman的angular生成器。Html5被捆绑在一起,可以实现livereload等功能。这是文档:
angular-generator-fullstack。
仅供参考,这是我的grunt文件:
'use strict';
module.exports = function (grunt) {
require('load-grunt-tasks')(grunt);
require('time-grunt')(grunt);
grunt.initConfig({
yeoman: {
app: require('./bower.json').appPath || 'app',
dist: 'public',
views: 'views'
},
express: {
options: {
port: process.env.PORT || 9000
},
dev: {
options: {
script: 'server.js'
}
},
prod: {
options: {
script: 'server.js',
node_env: 'production'
}
}
},
open: {
server: {
url: 'http://127.0.0.1:<%= express.options.port %>'
}
},
watch: {
js: {
files: ['{.tmp,<%= yeoman.app %>}/scripts/{,*/}*.js'],
tasks: ['newer:jshint:all']
},
jsTest: {
files: ['test/spec/{,*/}*.js'],
tasks: ['newer:jshint:test', 'karma']
},
livereload: {
files: [
'<%= yeoman.app %>/<%= yeoman.views %>/{,*//*}*.{html,jade}',
'{.tmp,<%= yeoman.app %>}/styles/{,*//*}*.css',
'{.tmp,<%= yeoman.app %>}/scripts/{,*//*}*.js',
'<%= yeoman.app %>/images/{,*//*}*.{png,jpg,jpeg,gif,webp,svg}'
],
options: {
livereload: true
}
},
compass: {
files: ['<%= yeoman.app %>/styles/{,*/}*.{scss,sass}'],
tasks: ['compass']
},
express: {
files: [
'server.js',
'lib/{,*//*}*.{js,json}'
],
tasks: ['express:dev'],
options: {
livereload: true,
nospawn: true
}
},
styles: {
files: ['<%= yeoman.app %>/styles/{,*/}*.css'],
tasks: ['newer:copy:styles', 'autoprefixer']
},
gruntfile: {
files: ['Gruntfile.js']
}
},
jshint: {
options: {
jshintrc: '.jshintrc',
reporter: require('jshint-stylish')
},
all: [
'<%= yeoman.app %>/scripts/{,*/}*.js'
],
test: {
options: {
jshintrc: 'test/.jshintrc'
},
src: ['test/spec/{,*/}*.js']
}
},
clean: {
dist: {
files: [{
dot: true,
src: [
'.tmp',
'<%= yeoman.views %>/*',
'<%= yeoman.dist %>/*',
'!<%= yeoman.dist %>/.git*'
]
}]
},
heroku: {
files: [{
dot: true,
src: [
'heroku/*',
'!heroku/.git*',
'!heroku/Procfile'
]
}]
},
server: '.tmp'
},
autoprefixer: {
options: {
browsers: ['last 1 version']
},
dist: {
files: [{
expand: true,
cwd: '.tmp/styles/',
src: '{,*/}*.css',
dest: '.tmp/styles/'
}]
}
},
compass: {
options: {
sassDir: '<%= yeoman.app %>/styles',
cssDir: '.tmp/styles',
imagesDir: '<%= yeoman.app %>/images',
javascriptsDir: '<%= yeoman.app %>/scripts',
fontsDir: '<%= yeoman.app %>/styles/fonts',
importPath: '<%= yeoman.app %>/bower_components',
relativeAssets: true
},
dist: {},
server: {
options: {
debugInfo: true
}
}
},
rev: {
dist: {
files: {
src: [
'<%= yeoman.dist %>/scripts/{,*/}*.js',
'<%= yeoman.dist %>/styles/{,*/}*.css',
'<%= yeoman.dist %>/images/{,*/}*.{png,jpg,jpeg,gif,webp,svg}',
'<%= yeoman.dist %>/styles/fonts/*'
]
}
}
},
useminPrepare: {
html: ['<%= yeoman.app %>/<%= yeoman.views %>/index.html',
'<%= yeoman.app %>/<%= yeoman.views %>/index.jade'],
options: {
dest: '<%= yeoman.dist %>'
}
},
usemin: {
html: ['<%= yeoman.views %>/{,*/}*.html',
'<%= yeoman.views %>/{,*/}*.jade'],
css: ['<%= yeoman.dist %>/styles/{,*/}*.css'],
options: {
assetsDirs: ['<%= yeoman.dist %>']
}
},
imagemin: {
dist: {
files: [{
expand: true,
cwd: '<%= yeoman.app %>/images',
src: '{,*/}*.{png,jpg,jpeg,gif}',
dest: '<%= yeoman.dist %>/images'
}]
}
},
svgmin: {
dist: {
files: [{
expand: true,
cwd: '<%= yeoman.app %>/images',
src: '{,*/}*.svg',
dest: '<%= yeoman.dist %>/images'
}]
}
},
htmlmin: {
dist: {
options: {
},
files: [{
expand: true,
cwd: '<%= yeoman.app %>/<%= yeoman.views %>',
src: ['*.html', 'partials/*.html'],
dest: '<%= yeoman.views %>'
}]
}
},
ngmin: {
dist: {
files: [{
expand: true,
cwd: '.tmp/concat/scripts',
src: '*.js',
dest: '.tmp/concat/scripts'
}]
}
},
cdnify: {
dist: {
html: ['<%= yeoman.views %>/*.html']
}
},
copy: {
dist: {
files: [{
expand: true,
dot: true,
cwd: '<%= yeoman.app %>',
dest: '<%= yeoman.dist %>',
src: [
'*.{ico,png,txt}',
'.htaccess',
'bower_components/**/*',
'images/{,*/}*.{webp}',
'fonts/*'
]
}, {
expand: true,
dot: true,
cwd: '<%= yeoman.app %>/<%= yeoman.views %>',
dest: '<%= yeoman.views %>',
src: '**/*.jade'
}, {
expand: true,
cwd: '.tmp/images',
dest: '<%= yeoman.dist %>/images',
src: [
'generated/*'
]
}]
},
heroku: {
files: [{
expand: true,
dot: true,
dest: 'heroku',
src: [
'<%= yeoman.dist %>/**',
'<%= yeoman.views %>/**'
]
}, {
expand: true,
dest: 'heroku',
src: [
'package.json',
'server.js',
'lib/**/*'
]
}]
},
styles: {
expand: true,
cwd: '<%= yeoman.app %>/styles',
dest: '.tmp/styles/',
src: '{,*/}*.css'
}
},
concurrent: {
server: [
'copy:styles'
],
test: [
'copy:styles'
],
dist: [
'copy:styles',
'imagemin',
'svgmin',
'htmlmin'
]
},
karma: {
unit: {
configFile: 'karma.conf.js',
singleRun: true
}
}
});
grunt.registerTask('express-keepalive', 'Keep grunt running', function() {
this.async();
});
grunt.registerTask('serve', function (target) {
if (target === 'dist') {
return grunt.task.run(['build', 'express:prod', 'open', 'express-keepalive']);
}
grunt.task.run([
'clean:server',
'compass:server',
'concurrent:server',
'autoprefixer',
'express:dev',
'open',
'watch'
]);
});
grunt.registerTask('server', function () {
grunt.log.warn('The `server` task has been deprecated. Use `grunt serve` to start a server.');
grunt.task.run(['serve']);
});
grunt.registerTask('test', [
'clean:server',
'compass',
'concurrent:test',
'autoprefixer',
'karma'
]);
grunt.registerTask('build', [
'clean:dist',
'compass:dist',
'useminPrepare',
'concurrent:dist',
'autoprefixer',
'concat',
'ngmin',
'copy:dist',
'cdnify',
'cssmin',
'uglify',
'rev',
'usemin'
]);
grunt.registerTask('heroku', [
'build',
'clean:heroku',
'copy:heroku'
]);
grunt.registerTask('default', [
'newer:jshint',
'test',
'build'
]);
};
关于 Express 服务器的配置:
'use strict';
var express = require('express'),
path = require('path');
var app = express();
app.configure('development', function(){
app.use(require('connect-livereload')());
app.use(express.static(path.join(__dirname, '.tmp')));
app.use(express.static(path.join(__dirname, 'app')));
app.use(express.errorHandler());
app.set('views', __dirname + '/app/views');
});
app.configure('production', function(){
app.use(express.favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(express.static(path.join(__dirname, 'public')));
app.set('views', __dirname + '/views');
});
app.configure(function(){
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
});
var api = require('./lib/controllers/api'),
controllers = require('./lib/controllers');
app.get('/api/awesomeThings', api.awesomeThings);
app.get('/partials/*', controllers.partials);
app.get('/*', controllers.index);
var port = process.env.PORT || 3000;
app.listen(port, function () {
console.log('Express server listening on port %d in %s mode', port, app.get('env'));
});
Gruntfile.js
中添加第一个答案代码吗?我已经从https://npmjs.org/package/grunt-connect-rewrite安装了`grunt-connect-rewrite`。但我不知道在哪里添加它到我的Gruntfile.js文件中。 - iCode