Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular url is adding unwanted characters

I had a project where the URL just worked fine when working locally by going to

localhost:9000/

the URL would become

http://localhost:9000/#/

As of some change I made it now goes to

http://localhost:9000/#!/ ( with an exclamation mark )

Also, other URLs become weird. if I try to click on a link that for example, goes to the dashboard. It doesn't take me there. Instead, it makes the URL like

/#!/#%2Fdashboard

and nothing happens after that. What did I do wrong and how can I possibly solve this? I can't show any code since I don't know where I went wrong. I followed the following tutorial and after that, the problem occurred. Perhaps the mistake is there?

tutorial link

I added my .config where I set up the routes.

  .config(function ($routeProvider, $httpProvider) {
$routeProvider
  .when('/', {
    templateUrl: 'views/main.html',
    controller: 'MainCtrl',
    controllerAs: 'vm',
    activetab: 'main'
  })
  .when('/about', {
    templateUrl: 'views/about.html',
    controller: 'AboutCtrl',
    controllerAs: 'vm',
    activetab: 'about'
  })
  .when('/faq', {
    templateUrl: 'views/faq.html',
    controller: 'FaqCtrl',
    controllerAs: 'vm',
    activetab: 'faq'
  })
  .when('/dashboard', {
    templateUrl: 'views/dashboard.html',
    controller: 'DashboardCtrl',
    controllerAs: 'vm',
    activetab: 'dashboard'
  })
  .when('/logout', {
    templateUrl: 'views/main.html',
    controller: 'LogoutCtrl',
    controllerAs: 'vm'
  })
  .otherwise({
    redirectTo: '/'
  });
  $httpProvider.interceptors.push(['$q', '$window', '$localStorage', function($q, $window, $localStorage) {
    return {
      'request': function (config) {
        config.headers = config.headers || {};
        if ($localStorage.globals) {
          config.headers.access_token = $localStorage.globals.currentUser.token;
        }
        return config;
      },
      'responseError': function(response) {
        switch(response.status) {
          case 403:
            //$window.location = '/'
            break;
          case 404:
            //$window.location = './404.html';
            break;
          case 500:
            $window.location = './500.html';
            break;
        }
        return $q.reject(response);
      }
    };
  }]);
like image 664
Reshad Avatar asked Dec 23 '22 22:12

Reshad


2 Answers

This is actually not a bug.

See commit-aa077e8

The default hash-prefix used for $location hash-bang URLs has changed from the empty string ('') to the bang ('!').

If you actually want to have no hash-prefix, then you can restore the previous behavior by adding a configuration block to your application:

appModule.config(['$locationProvider', function($locationProvider) {
  $locationProvider.hashPrefix('');
}]);
like image 178
Modzful Avatar answered Dec 26 '22 19:12

Modzful


I have managed to solve this problem by using

$locationProvider.hashPrefix('');

It seems to be a bug in 1.6.0 angularjs

like image 32
Reshad Avatar answered Dec 26 '22 17:12

Reshad