我想用 angularJS 做服务器端渲染。是否可以在旧版本的 angularJS(2.0 之前的版本)中执行此操作?
最佳答案
有一些 npm 模块可以在服务器端解析 AngularJS 模板:
AngularJS-Server is built on jsdom, an implementation of the DOM API for Node, and angularcontext, which provides a lightweight container in which to instantiate your AngularJS application.
After I eventually got a very basic AngularJS example running on the server, I moved on to routing. The first problem I ran into was that jsdom just stubs out the Window object's XMLHttpRequest, so it doesn't do anything. Solution was pretty simple -- modify a line in lib/jsdom/browser/index.js:
XMLHttpRequest: require('../../../../xmlhttprequest').XMLHttpRequest
Your angular app must use html5mode. The reason behind this requirement is that browsers don't send the hashbang fragment to the server.
So for example, rendering on the server side http://domain.com/url#blah will just render http://domain.com/url.
The library exposes several angular services, which will let you compile angular templates inside node
引用资料
关于javascript - 是否可以在 Angular JS 中进行服务器端渲染(在旧版本中,即 2.0 之前的版本)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35174642/