这是working plunker based on your code. 您缺少 ngRoute 将根据您的配置替换的 ng-view。所以,index.html 看起来像:
<body ng-controller="MainCtrl">
<p>Hello {{name}}!</p>
<ng-view></ng-view>
</body>
ng-view 是一个 Angular 指令,它将在主布局文件中包含当前路由的模板(/main 或 /home/student)。简而言之,它根据路由获取文件并将其注入主布局(index.html)。
在配置中,ng-view 将替换为指向 Login.html 的“main”。我将“/home/student/”更改为指向新页面“dic.html”以避免无限循环,因为它曾经指向 index.html
var app = angular.module('plunker', ['ngRoute', 'Controllers']);
app.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/main', {
templateUrl: 'Login.html',
controller: 'LoginCtrl'
}).
when('/home/student', {
templateUrl: 'dic.html',
controller: 'DictionaryController'
}).
otherwise({
redirectTo: '/main'
});
}
]);
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
});
像您的示例一样,如果使用“harish”作为电子邮件登录并使用“harish”作为密码登录,则会调用 successCallback 并转到将 ng-view 替换为 dic.html 的“/home/student” :
$scope.validate = function() {
$http.get('credentials.json').then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
console.log('Data: ' + JSON.stringify(response));
$scope.users = response.data;
var count = 0;
for (var i = 0, len = $scope.users.length; i < len; i++) {
if ($scope.username === $scope.users[i].username && $scope.password === $scope.users[i].password) {
alert("login successful");
count = count + 1;
if ($scope.users[i].role === "student") {
$location.path('/home/student');
break;
}
}
}
if (count != 1) {
alert("Please provide valid login credentials");
$location.path("/main")
}
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
console.log("Error: " + JSON.stringify(response));
alert(JSON.stringify(response));
});
};
如果有帮助,请告诉我们。