[英]ng-route [HOW TO]
我嘗試遵循ng-route的官方角度文檔,但是我卻迷路了。 所以我希望你能給我建議。
我嘗試為具有2個控制器的現有簡單站點添加路由。 一種用於登錄,另一種用於顯示數據。
所以我在main.js中添加了以下內容:
var historyApp = angular.module('historyApp', ['ngRoute', 'LoginCtrl', 'HistoryCtrl']);
historyApp.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/login', {
templateUrl: 'templates/login.html',
controller: 'LoginCtrl'
}).
when('/history', {
templateUrl: 'templates/history.html',
controller: 'HistoryCtrl'
}).
otherwise({
redirectTo: '/login'
});
}]);
其中Loginctrl和HistoryCtrl是真正簡單的控制器,例如:
historyApp.controller('LoginCtrl', function ($scope, $http){
$scope.getToken = function(){
$http({
method: 'POST',
url: '../../api/v1/Oauth.php',
data: { login : $scope.username, password : $scope.password }
}).then(function successCallback(response) {
console.log(response);
}, function errorCallback(response) {
console.log(response);
});
};
});
因此設置了配置,並且控制器和模板存在(肯定),我也將ng-route.js添加到了login.html和history.html
但是,當我打開localhost/myProject/#/login
以及localhost/myProject#/login
我沒有重定向到登錄頁面,它基本上只是顯示我的根目錄。
這部分是如何工作的? 是從根目錄還是其他地方? 還是我做錯了什么?
確保頁面中有ng-view
指令:
<!doctype html>
<html lang="en" ng-app="phonecatApp">
<head>
...
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/angular-route/angular-route.js"></script>
<script src="js/app.js"></script>
<script src="js/controllers.js"></script>
</head>
<body>
<div ng-view></div>
</body>
</html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.