Angular -routing, direct URL redirection

I have an angular application defined in my file index.html. Using angular -routing, I route the link with the name /erezto load the view using the template. It works inside the application - when I click the link /erezon the navigation bar on index.html, it works great. But when I go directly to my.site.com/erez in the address bar, it gives 404. I understand why this is so without server-side code, but is there a clean angular way to achieve direct URLs? my routing code:

var app = angular.module('labApp', ['ngRoute', 'angular.filter']);

app.config(function ($routeProvider, $locationProvider) {
    $routeProvider.
        when('/', {
            templateUrl: 'index.html',
            controller: 'mainCtrl'
        }).
        when('/erez', {
            templateUrl: 'erez2.html',
            controller: 'erezCtrl'
        }).
        otherwise({
            redirectTo: '/'
        });

    $locationProvider.html5Mode(true);
});
+4
source share
2 answers

:

url :

<base href="/" />

, . :

$locationProvider.html5Mode({
  enabled: true,
  requireBase: false
});

, .

+2

, .

var app = angular.module('labApp', ['ngRoute', 'angular.filter']);

    app.config(function ($routeProvider, $locationProvider) {
        $routeProvider.
            when('/', {
                templateUrl: 'index.html',
                controller: 'mainCtrl'
            }).
            when('/erez', {
                templateUrl: 'erez2.html',
                controller: 'erezCtrl'
            }).
            otherwise({
                redirectTo: '/'
            });

        $locationProvider.html5Mode({ enabled: true, requireBase: false });
    });
+1

Source: https://habr.com/ru/post/1615124/


All Articles