0
  <script type="text/javascript">

angular.module('ngView', ['ngRoute'],
    function($routeProvider){
      $routeProvider.when('/test',
        {
          template: '{{}}',
          controller: function(){
            console.log('controller');
            $('div').first().html('<b>OK</b>');
          }
        }
      );
    }
  );
  </script>

This is all my angular code. In URL, i can access test by

file:///Users/tom/Documents/Projects/a4/index2.html#/test
file:///Users/tom/Documents/Projects/a4/index2.html#test

Both of the above two ways work.

But when I change #/test to /test, it doesn't work.

How do I configure Angularjs to only use '/' in URL path?

4
  • 3
    You need to enable HTML5 model in config state $locationProvider.html5Mode(true). Please see the guide here docs.angularjs.org/guide/$location Commented Apr 18, 2014 at 8:57
  • See here: stackoverflow.com/questions/14319967/… Commented Apr 18, 2014 at 8:59
  • Thanks! I checked this HTML5 question before asking this question, but didn't think it is related to my situation. Commented Apr 18, 2014 at 9:30
  • I tried, but it doesn't work for me... strange... Commented Apr 18, 2014 at 11:19

1 Answer 1

2

I see that you are working locally, you need a server to work with html5Mode, after enabling it you need to define an .htaccess or a virtual host on the server take a look at this question AngularJS: can't get html5 mode urls with ui-route $state

<VirtualHost *:80>
    ServerName my-app

    DocumentRoot /path/to/app

    <Directory /path/to/app>
        RewriteEngine on

        # Don't rewrite files or directories
        RewriteCond %{REQUEST_FILENAME} -f [OR]
        RewriteCond %{REQUEST_FILENAME} -d
        RewriteRule ^ - [L]

        # Rewrite everything else to index.html to allow html5 state links
        RewriteRule ^ index.html [L]
    </Directory>
</VirtualHost>
Sign up to request clarification or add additional context in comments.

3 Comments

I am using nodejs, and I didn't find any .htaccess in the project folder. Does this solution still apply to nodejs? HTML5 is a browser side technology, why do i need to configure server?
I put the project files on a local nodejs server running express4 MVC framework. Here is the access URL: localhost:3000/a4/test . It still won't work. (no .htaccess file) If you are familar with nodejs server, can you please tell me what I missed? Thanks!
Unfortunately, I never had any experience with nodejs advanced configurations but maybe you could use something from these questions stackoverflow.com/questions/17199095/… stackoverflow.com/questions/17777967/… stackoverflow.com/questions/13222252/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.