使用Angular.js路由时出现404错误

use*_*587 1 javascript html5 angularjs firebase angularfire

我正在尝试使用angular和angularfire设置路由,但遇到了麻烦.

var app = angular.module('sampleApp', ['ngRoute', 'firebase', 'ui.bootstrap']);

app.config(function ($routeProvider) {
    $routeProvider
      .when('/', {
        templateUrl: 'index.html',
        controller: 'SampleCtrl'
      })
      .when('/login', {
        templateUrl: 'views/login.html',
        controller: 'LoginCtrl'
      })
      .otherwise({
        redirectTo: '/'
      });

  });

app.controller('LoginCtrl', function($scope, $firebase){
  console.log('LoginCtrl')
})
Run Code Online (Sandbox Code Playgroud)

当我尝试命中localhost:8000/login时收到以下错误消息

Error code explanation: 404 = Nothing matches the given URI.
Run Code Online (Sandbox Code Playgroud)

如何使用ngRoute和AngularFire设置简单路线?

Dav*_*ast 5

使用默认路由器时,您需要在URL中使用哈希.

localhost:8000/#/login
Run Code Online (Sandbox Code Playgroud)

如果你想删除哈希,那就是另一个故事.

你需要使用$locationProvider和设置html5Mode()为true.从那里你将不得不停止使用$window.location.href改变路线和使用$location.path().

然后,您必须获得服务器或使用支持URL重写的Firebase Hosting等服务.

  • Angular.js教程中介绍了此材料.在深入研究AngularFire(即Firebase + Angular)之前,这是一个很好的入门 (2认同)