我有一个母版页(Index.html),我正在使用
<div>
<ul class="nav navbar-nav navbar-right">
<li><a ui-sref="signout.searchAsset"> Sign Out</a></li>
<li><a ui-sref="signin.searchAsset"> Sign In</a></li>
<li><a href="#"></a></li>
<li><h4 >Headset - Signin ({{$scope.UserFullname}})</h4></li>
</ul>
</div>
<div class="container">
<!-- views will be injected here -->
<div ui-view></div>
</div>
Script.js文件 - &gt;
angular.module('aegis', ['ngAnimate', 'ui.router', 'ngSignaturePad', 'ngDialog', 'ui.select', 'ngStorage'])
// configuring our routes
// =============================================================================
.config(function ($stateProvider, $urlRouterProvider) {
$stateProvider
// route to show our basic form (/form)
.state('signout', {
url: '/signout',
templateUrl: 'app/components/headset/view-signout-form.html',
controller: 'SignoutController'
})
// nested states
// each of these sections will have their own view
// url will be nested (/form/profile)
.state('signout.searchAsset', {
url: '/searchAsset',
templateUrl: 'app/components/headset/view-signout-search-asset.html'
})
// url will be /signout/searchEmployee
.state('signout.searchEmployee', {
url: '/searchEmployee',
templateUrl: 'app/components/headset/view-signout-search-employee.html')
.state('index', {
url: '/index',
templateUrl: 'index.html'
Controller : 'loginController'
})
.controller('SignoutController', function ($scope, $http, $state, ngDialog, $location, $window, appConfig, $localStorage, AuthService) {
$scope.formData = {};
$scope.formData.accepted = false;
$scope.formData.assetFound = false;
}).controller('LoginController', function ($scope, $http, $state, appConfig, $localStorage, AuthService) {
$scope.UserFullname = AuthService.CurrentSession().Fullname;
AuthService.getSession();
$scope.login = function () {
AuthService.login($scope.username, $scope.password);
}
});
当我第一次登录我的页面转到index.html然后显示$ scope.UserFullname id中的值,但是当我点击任何链接(登录)时,状态发生变化,$ scope.UserFullname中的值为空。即使状态发生变化,我如何维护$ scope.UserFullname中的值?
答案 0 :(得分:0)
你好进入你的登录状态你可以使用解决和返回你的服务的承诺 (身份验证服务),使用用户名 您可以通过解决方案在每个状态下调用您的身份验证服务。你的应用程序,所以你可以注入它并在每个控制器上使用它。
像这样:
.state('index', {
url: '/index',
templateUrl: 'index.html'
Controller : 'loginController',
// Example using function with returned promise.
// This is the typical use case of resolve.
// You need to inject any services that you are
// using, e.g. $http in this example
user: function($http){
// $http returns a promise for the url data like username
return $http({method: 'GET', url: '/someUrl'});
},
})
controller('LoginController', function (user, $scope, $http, $state, appConfig, $localStorage, AuthService) {
//the returned promise of state 'index' (**user**), is injected into that controller
$scope.UserFullname = user;
});
希望有所帮助,祝你好运。