简体   繁体   English

如何在angular js中的ui-state更改后维护母版页中使用的控制器的范围变量?

[英]How to maintain scope variable of a controller used in master page after the ui-state changes in angular js?

I have a master page(Index.html) in which I'm using我有一个母版页(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 file--> Script.js 文件-->

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);
         }     

});

When i first login my page goes to index.html then the value in $scope.UserFullname id displayed but when i click on any link(Sign in),the state changes and the value in $scope.UserFullname is empty.当我第一次登录时,我的页面转到 index.html 然后 $scope.UserFullname id 中的值显示但是当我单击任何链接(登录)时,状态发生变化并且 $scope.UserFullname 中的值为空。 How can i maintain the value in $scope.UserFullname even if the state changes?即使状态发生变化,我如何维护 $scope.UserFullname 中的值?

Hello into your login state you can use resolve and return a promise of your service ( authentication service ) with the username .您好,进入您的登录状态,您可以使用resolve并使用用户名返回您的服务身份验证服务的承诺
You can do a resolve that calls your authentication service on each 'state' of your application, so you can inject it and use it on each controller.您可以执行一个解析,在应用程序的每个“状态”上调用您的身份验证服务,以便您可以注入它并在每个控制器上使用它。

Like this:像这样:

 .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'});
         },
          })

inject resolve object into your controller:将解析对象注入控制器:

. .

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;
});

Hope helps , good luck.希望有帮助,祝你好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM