Express + AngularJS + HTML: ng-include not working (404 - Page not found error)

风格不统一 提交于 2019-12-04 09:20:09

ng-include is a client side include and as such the path to the included html file is relative to the client perception of the url.

Since jade is abstracting your folder structure and does not provide direct access to your views folder you should probably put the included html file on the public folder just as any externaly accessible file.

When you include the footer in your .jade file (As per Note 2) you are doing a server side include which uses the server directory structure.

I also had similar problem and I'm also new in Angular and Node :) I'm not sure that my solution is safe, so if itsn't let me know about that.

I've solved it by expose partial directory with express.static middleware.

So add something like that to the app.js:

app.use(require('less-middleware')(path.join(__dirname, 'public'))); //common
app.use('/views', express.static(__dirname + '/views')); //serve views directory as assets

Then you can access your partial from client side:

<div ng-include="'/views/footer.html'"></div>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!