3

When I try to change the location within my controller it loads server's root, not the certain page:

'use strict';

eventsApp.controller('TestController',
    function TestController($scope, $location){
        console.log('test comes here!');
        $scope.makeTest = function(){
            $location.url('/test');
        }
});

I have a router like this:

'use strict';

var eventsApp = angular.module('eventsApp', ['ngResource','ngSanitize'])
    .config(function($routeProvider, $locationProvider){
        $routeProvider
          .when('/test',{
            templateUrl:'templates/test.html',
            controller:'TestController'
        }).otherwise({
            templateUrl:'templates/default.html',
            controller:'DefaultController',
            redirectTo:''
        });
        $locationProvider.html5Mode(true);
    });

In my index.html I set the directive

<base href="/">

It seems that everytime it implements otherwise. However if I use

location.href = '/test';

instead of

$location.url('/test');

, then it works. I have no idea about such a behavior. Do you?

srgg6701
  • 1,878
  • 6
  • 23
  • 37

2 Answers2

3

Though this question relates to state here's how you can navigate to another url:

$location.url(myUrl);

This should work but I've seen some issues with it - i.e. it will call the code but just not go anywhere. If this is the case you could also use the $window service like so:

$window.location.assign(url);
Katana24
  • 8,706
  • 19
  • 76
  • 118
1

if you have a $scope.apply() already running, consider using $timeout as a workaround.

Not perfect, but shall do the job. Better case: set a global variable which you set to "loaded" once your api are done loading, then update your function/call scope related actions.

Xspirits
  • 217
  • 2
  • 7