1

I have installed my Angular App in a location like this: http://example.com/my-app

My App routing is like this:

var myApp = angular.module('myApp', ['ngRoute','ngAnimate', 'ui.bootstrap', 'angularFileUpload']);

myApp.config(['$routeProvider', function($routeProvider) {  

    $routeProvider
        .when('/templates', {
            controller: 'TemplatesController',          
            templateUrl: '/components/com_myApp/myApp/views/templates/template-list.html'
        })  

        .when('/apps', {
            controller: 'AppController',            
            templateUrl: '/components/com_myApp/myApp/views/apps/app-list.html'
        })

        .otherwise({
            redirectTo: '/templates'    
        }); 

}]);

Now what happens is, when I go to http://example.com/my-app, the url instead of showing http://example.com/my-app#/templates it is showing as http://example.com/templates

It seems the otherwise condition is basically removing the base directory my-app# from the url. I have been scratching my head over this and after reading I also tried adding base url to head tag and tried changing the myApp.config to this:

myApp.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {  
    $locationProvider.html5Mode(true);
....
}

But although the routing seems to work when using $locationProvider like the above, the base url is still not showing the way I want it. The url is showing like this http://example.com/templates without my-app in the url path. I dont want Angular to remove the base path from the URL and I want the urls to show like this http://example.com/my-app/..xyz...

Why is Angular doing this?

2
  • Hey have you used <base href="/my-app/" /> i mean both side backslash in base tag ..?? Commented Feb 1, 2015 at 14:41
  • My base url is created automatically by Joomla like this <base href="http://example.com/my-app" /> Commented Feb 1, 2015 at 15:32

1 Answer 1

2

This is happening because you've instructed Angular to not use hashbang URLs by specifying $locationProvider.html5Mode(true). Remove or otherwise comment out that code snippet and if you specified <base href="/"> in your root template file, remove or comment out that also and just use ngRoute without those.

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

4 Comments

#Rookie I tried Commenting $locationProvider.html5Mode(true) and base hrefbut it doesnt help. Also my first sample above does not use locationProvider and that has the same problem too. Routing works when I visit angular pages like #/templates but when I visit the default page http://example.com/my-app, instead of the url showing http://example.com/my-app/#templates/ it shows http://example.com/templates. This is the case in both examples I've shown above (with and without $locationProvider). My base url is created automatically by Joomla like this http://example.com/my-app
Also adding to that, when I go to http://example.com/my-app, the url in the browser shows this for like half a second http://example.com/my-app#/templates and then it immediately changes to http://example.com/templates. The view is showing the template-list.html page correctly but the url for some reason shows the correct one first and then changes quickly to http://example.com/templates. That is strange.
Okay. It appears the problem isn't with Angular but rather how your Joomla backend is serving the template that handles your root url -- /my-app. I don't have any experience with Joomla but in general, the backend serves that index template as a static HTML file containing all your JS scripts (including Angular) in <script> tags and those JS files are what will enable Angular take over the routing from the backend (i.e the routing is handled entirely client-side). Hopefully that might point you in the right direction.
Makes sense @Rookie. I will dig deeper from the Joomla end and see why this odd behaviour, I added this Angular js as a Joomla component and created a menu item for it. What you say does make sense. I will update back if I was able to identify the issue (hopefully by tomorrow). Thanks again for reading my question and writing to me after looking at my Angular settings. I appreciate that :)

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.