Do angular views work when a site is served from the local file system?

荒凉一梦 提交于 2019-11-28 02:01:35
forivall

For routing & ajax (& more) to work properly, run a local development server; avoid use of file:// for development, since browsers have different rules for it.

Tools like yeoman + generator-angular will automatically set up a gruntfile with a server task, which will run a node-connect server to locally serve your files.

You can do this with

  • python: (3)python -m http.server 8001 (replace http.server with SimpleHttpServer in 2)
  • node.js + connect
  • ruby + rack
  • From the angularjs tutorial (number 5 under "working with the code") - "You need an http server running on your system, but if you don't already have one already installed, you can use node to run scripts\web-server.js, a simple bundled http server."

Response from comments: For phonegap, use the phonegap tools. It does exactly what I said, it runs a local server.

This will work.

angular.module('MainModule', []).config(function($locationProvider, $routeProvider) {
    $locationProvider.hashPrefix("!");
    $locationProvider.html5Mode(false);
    $routeProvider.when('/', {template: './js/templates/home.html', controller:HelloWorldCtrl});
    $routeProvider.when('/other', {template: './js/templates/other.html'});
});

In index HTML you need to specify templates:

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