【发布时间】:2016-05-03 18:20:50
【问题描述】:
我创建了一个用于身份验证的登录页面。根据认证结果,重定向到不同的页面。
//controller.js
app.controller("LoginCtrl", ['$scope', '$location', 'loginFactory', function($scope, $location, loginFactory){
$scope.authenticate = function() {
loginFactory.login($scope.username, $scope.password)
.then(function(response) {
$location.path('/home');
}, function errorCallBack(response) {
console.log("Failed auth");
$location.path('/login');
});
}
}]);
应用与路由一起定义在下面的 config.js 中。 //config.js
var app = angular.module('ristoreApp', ['ngRoute']);
app.config(function ($routeProvider, $locationProvider, $httpProvider) {
$routeProvider
.when('/', {
redirectTo: '/home'
})
.when('/login', {
templateUrl: 'views/login.html',
controller: 'LoginCtrl'
})
.when('/home', {
templateUrl: 'views/home.html',
})
.otherwise({
redirectTo: '/login'
});
});
文件结构:
root/
js/ -- config.js
-- authentication/ -- controller.js
views/ -- home.html
-- login.html
当我提交登录表单时,它不会重定向到“http://127.0.0.1:8081/views/login”,而是重定向到“http://127.0.0.1:8081/views/login#/login”。加上“http://127.0.0.1:8081/login”返回 404。
我在 root 下启动 node.js。从 Chrome 的“网络”选项卡中,它显示“config.js”已加载。为什么路由不起作用?
【问题讨论】:
标签: angularjs angularjs-routing