我正在尝试从“public”文件夹中的角度路由文件(app > public > main.js)调用“views”文件夹中的ejs文件(app > views > list.ejs)。但 list.ejs 未加载。
/************main.js****************/
var fsApp=angular.module('chart-app');
fsApp.config(function($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'list.ejs',
controller: 'listCtrl'
})
/*In my server.js server file I am calling the files in public folder this way: */
app.use('/static',express.static(__dirname + '/public'));
app.get('/',function(req,res){
res.render('index.ejs');
});
<!--In index.ejs (where my <ng-view></ng-view> is) I am including the link for static files:-->
<head>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js"></script>
<script src="/static/angular-route.js"></script>
<script src="/static/main.js"></script>
</head>
<body ng-app="chart-app" class="container-fluid">
<div ng-controller="fairshareCtrl">
<div class="row" ng-view></div>
</div>
</body>
我还需要对服务器文件做些什么吗?我错过了什么?
最佳答案
我不知道你在后端使用什么,但是,当你从express渲染ejs文件时,你必须为其定义引擎。
app.use(express.static(path.join(__dirname, 'public')));
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
然后你就可以使用
app.get('*',function(req,res){
res.render('index.ejs');
});
并将所有角边保存在公共(public)文件夹中。
或者您可以只加载 html 文件,而不是通过 ejs 加载 ejs
app.engine('html', require('ejs').renderFile);
app.get('*', (req, res) => {
res.render('index.html');
})
关于node.js - Angularjs从express.js的 "views"文件夹中路由 "public"文件夹中的ejs静态文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44449889/