简体   繁体   中英

Angular Js routing using $routeProvider

I am using angular js routing using $routeProvider but when I reload the page I get error message like "Cannot GET /home". My code is as follows

.when("/", {
  templateUrl: "views/login/login.html",
  controller: "LoginController as loginCtrl"
})

.when("/home", {
  templateUrl: "views/home/home_template.html",
  controller: "HomeController as homeCtrl"
});

when I reload page from home then I get this error message . Also I want to keep this login outside routing so what practice should I follow

Try this

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

window.routes =
    {
        '/': {
            templateUrl: 'app/visitor/visitor.html',
            anonymous: true
        },
        '/index': {
            templateUrl: 'app/visitor/visitor.html',
            caseInsensitiveMatch: true,
            anonymous: true
        },
        '/lu/:mt?/:tab?/:id?': {
            templateUrl: 'app/user/user.html',
            caseInsensitiveMatch: true,
            anonymous: false
        }
    };

app.config(function ($routeProvider, $locationProvider) {
    for (var path in window.routes) {
      $routeProvider.when(path, window.routes[path]);
    }
    $routeProvider.otherwise({redirectTo: '/'});

    $locationProvider.html5Mode(true);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM