Does Angular need a web server to run? So would Angular work if I give browser a url of a local file?
You cannot just access an angular application by the filepath on the local machine because you will get cross origin domain errors.
The solution is to install http-server (which requires node.js to be installed). This allows you to create a http-server local to your machine and will allow you to access the Angular application as if it were hosted online for development and test purposes.
Yes you can run a local file, but if you need data off a server, the browser should block it, depending on what version and type of browser you are running.
Here is the official Angularjs Tutorial explanation under the PhoneCat Tutorial App: Running the Development Web Server
While Angular applications are purely client-side code, and it is possible to open them in a web browser directly from the file system, it is better to serve them from an HTTP web server. In particular, for security reasons, most modern browsers will not allow JavaScript to make server requests if the page is loaded directly from the file system.
If you need to just display data using an expression like {{mymessage}}
inside a div, you don't need a web server.
But if you need to load template
html files uing ngview
, you need a web server- otherwise it will complain with following error.
Request cannot load file. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.
If laoding templates is needed for learning angularjs routing, I found a web server
exe
easy to use - HFS. So far it meets my requirements for learning AngularJS.
References
- HFS:Introduction
- HTTP File Server