1

I am trying to modify the url of my angularjs app . Initially my urls were http:localhost:8080/a/web/app/index.html#/ and http:localhost:8080/a/web/app/index.html#/next

when I inserted the following code to my app.js

    var App =angular.module('App', [
     'ngCookies',
     'ngResource',
     'ngRoute',
    ])
   .config(function ($routeProvider,$locationProvider) {
    $routeProvider
    .when('/', {
    templateUrl: '/a/web/app/views/main.html',
    controller: 'ctrl_main'
    })
   .when('/next', {
    templateUrl: '/a/web/app/views/next.html',
    controller: 'ctrl_next'
    });        

  $locationProvider.html5Mode(true);
  });

My new urls became http:localhost:8080 and http:localhost:8080/next . My problem is when I tried to reload the page at http:localhost:8080/next , 404 Not Found error is coming

2 Answers 2

1

That's expected with html5 mode. The browser, if you ask it to reload, will send a request to the URL it has in its location bar.

So you need to configure the server to actually send back the index.html page for all the bookmarkable URLs you use in the angular application. The whole page will reload, the angular app will restart, the $route service will restart, and will invoke the controller and display the partial configured for the URL.

Sign up to request clarification or add additional context in comments.

6 Comments

is there any way to redirect to the same page were reload is made , rather than index.html from server?
That's what I explained in my answer. Re-read it. The browser will load /foo/bar from the server. The server will send back the contents of the index.html page. The angular app will reload, and the router will display the page of the application that is mapped to /foo/bar.
hi ,when I send back the index.html url from server, for a reload at /foo/bar ,as you said the angular app reloaded ,but now the $location.path is pointing to /a/web/app/index.html , not /foo/bar ,so it is loading the default(.otherwise) route
You shouldn't redirect to index.html. You should serve the contents of index.html directly from the URL /foo/bar.
Yeah, this is still not working for me. Is there anything special I need to do to my .htaccess file? This is my last rule RewriteRule ^ index.html [L], but my /admin/classes is getting confused when I reload the page. Here is my ui-router state for this url. $stateProvider. state('admin.classes',{ url:'/classes', templateUrl:'app/partials/admin/classes.html', controller:'AdminClassesCtrl' });
|
0

I know the answer is a bit outdated, but in my case adding <base> attribute to index.html helped, for example:

<base href="/a/web/app/">

Comments

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.