我有以下以及認證,這是通過PHP腳本和MySQL進行路由:angular.js:動態導航取決於登錄狀態
的app.config
app.config(['$routeProvider',
function ($routeProvider) {
$routeProvider.
when('/login', {
title: 'Login',
templateUrl: 'partials/login.html',
controller: 'authCtrl'
})
.when('/logout', {
title: 'Logout',
templateUrl: 'partials/login.html',
controller: 'logoutCtrl'
})
.when('/dashboard', {
title: 'Dashboard',
templateUrl: 'partials/dashboard.html',
controller: 'authCtrl'
})
.otherwise({
redirectTo: '/login'
});
}])
.run(function ($rootScope, $location, Data) {
$rootScope.$on("$routeChangeStart", function (event, next, current) {
$rootScope.authenticated = false;
Data.get('session').then(function (results) {
if (results.uid) {
$rootScope.authenticated = true;
$rootScope.uid = results.uid;
$rootScope.name = results.name;
$rootScope.email = results.email;
} else {
var nextUrl = next.$$route.originalPath;
if (nextUrl == '/signup' || nextUrl == '/login') {
} else {
$location.path("/login");
}
}
});
});
});
authCtrl
app.controller('authCtrl', function ($scope, $rootScope, $routeParams, $location, $http, Data) {
$scope.login = {};
$scope.signup = {};
$scope.doLogin = function (customer) {
Data.post('login', {
customer: customer
}).then(function (results) {
Data.toast(results);
if (results.status == "success") {
$location.path('dashboard');
}
});
};
$scope.logout = function() {
Data.get('logout').then(function (results) {
Data.toast(results);
$location.path('login');
});
}
});
現在我想根據登錄狀態更改導航。如果用戶登錄,應該有一個登出按鈕和指向儀表板的鏈接。如果用戶沒有登錄它看起來應該是這樣
樣品未登錄用戶
<header id="navigation">
<nav id="main">
<ul>
<li id="login"><a href="#/login" class="btn"><i class="fa fa-power-off"></i> Login</a></li>
</ul>
</nav>
</header>
<main ng-view>
Content goes here
</main>
什麼是創建導航的最佳方式?它應該是一個單獨的模板?
當你使用ui-router時,你能寫一個代碼示例嗎?我想我必須使用ui-router ... – user3142695