0

I'm working on a project Using AngularJS as front-end and Spring MVC as back-end. Also I used plugin ui-router. The base url looks like localhost:8080/PROJECT_NAME/#! For a better looking I'm trying to remove the /#!/ from url.

I failed to get a correct result from the common solution from internet:

// Insert this into Angularjs module config file
$locationProvider.html5Mode(true);

// Add this into META tag
<base href="/">

After doing this what I got is all resources crashed, so I added my project name:

<base href="PROJECT_NAME">

Resources are correctly fetched, however, the log shows:

Uncaught Error: [$injector:modulerr] Failed to instantiate module app due to:

(anonymous function)    @   angular.js:68
(anonymous function)    @   angular.js:4756
forEach @   angular.js:357
loadModules @   angular.js:4717
createInjector  @   angular.js:4639
doBootstrap @   angular.js:1838
angular.resumeBootstrap @   angular.js:1867
maybeBootstrap  @   hint.js:569

I don't know what to do next, may need your help, thanks!


Router code:

app.config(
    function ($stateProvider, $urlRouterProvider){
        $urlRouterProvider.otherwise("/");

        $stateProvider
            .state('index', {
                url: "/",
                templateUrl: "homeview"
            });

        // use the HTML5 History API
        $locationProvider.html5Mode(true);
    }
);
2
  • post your routing code? (maybe app.js) Commented Dec 13, 2016 at 6:38
  • Hi @Nagaveer Gowda I've posted on tail of my question body, it's quite standard. I supported that templateURL from spring MVC controller. Commented Dec 13, 2016 at 6:49

3 Answers 3

1

[1] Set html5Mode as true using below mentioned code,

.config(["$locationProvider", function($locationProvider) {
    $locationProvider.hashPrefix('');
    $locationProvider.html5Mode(true);
}])

[2] Set base url in index.html as follows,

<base href="/" />

[3] Create a controller to redirect urls to the index file

@RequestMapping(value = "/{[path:[^\\.]*}")
public String stalklist() {
    return "forward:/";
}
Sign up to request clarification or add additional context in comments.

Comments

0

That # notifies the AngularJS that there is no need to go to the server for that url instead go to ng-router.

So ideally you should not remove that.

Comments

0

use the $locationProvider module and set html5Mode to true.

angular.module('StudentView', []).
 config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {

$routeProvider.
  when('/student', {templateUrl: 'partials/studentList.html',   controller: StudentListCtrl}).
  when('/student/:studentId', {templateUrl: 'partials/studentDetail.html', controller: StudentDetailCtrl}).
  otherwise({redirectTo: '/phones'});

$locationProvider.html5Mode(true);

  }]);

1 Comment

Thanks. This is what I did, however, it looks like it goto the server instead of angular, and returned me 404.

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.