2016-12-17 49 views
0

我不確定我是否使用正確的方法,請諮詢:
我有一個AngularJS應用程序,當用戶登錄後,我將從登錄頁面重定向到所需頁面。如何使用ui路由器重新加載所有應用程序模塊?

問題:
登錄按鈕,個人檔案相片沒有更新,因爲這2個元素是內部模塊.RUN和index.html,因爲兩者都是在工具欄裏面 enter image description here

如何我可以重新加載主應用程序(我不確定這是一個好方法),如果你有任何其他想法,它會很好。

內.RUN我試圖讓登錄後更新的個人主頁上,它擁有的地位和PIC:

auth.authenticate(localStorageService.get('profile'), token) 
index.html中

我具備的要素:

<img ng-src="{{profile.picture}}" style=" 
        width: 48px; 
        margin: 2px; 
        border-radius: 48px; 
        border: 0px solid #ddd;"> 

只需手動重新加載頁面,上面的代碼將被執行,並且所有的都將按預期更新。

+1

可以使用您的注入權威性的服務,並顯示正確的元素基於身份驗證 – charlietfl

回答

1

你可以做到以下幾點:

  1. 創建UserService,並保持這個模塊中的所有用戶信息。

  2. 使用getter函數從服務中獲取數據。

  3. 進樣UserService模塊兩地 (其中你保持你的觀點的信息,在這種情況下,你的'.RUN()塊(?) 並在您更改用戶狀態和數據登錄服務或登錄 控制器)。

  4. 一旦用戶通過身份驗證,就更新UserService模塊,就是這樣。

的jsfiddle:https://jsfiddle.net/s9nma12m/9/

var myApp = angular.module('myApp', []); 
myApp.service('UserService', [function() { 
    var UserService = this; 
    var _isAuthenticated = false; 
    UserService.isAuthenticated = function() { 
     return _isAuthenticated; 
    }; 

    var _user = { 
     name: 'Guest2' 
    }; 
    UserService.getUserInfo = function() { 
     return _user; 
    }; 

    UserService.login = function() { 
     // some logic... 
     // update user data here: 
     _user = { 
      name: 'Peter Griffin' 
     }; 
     _isAuthenticated = true; 
    }; 
}]); 

myApp.controller('mainCtrl', ['$scope', 'UserService', 
    function ($scope, UserService) { 
     $scope.isAuth = UserService.isAuthenticated; 
    }]); 

myApp.controller('headerCtrl', ['$scope', 'UserService', 
    function ($scope, UserService) { 
     $scope.isAuth = UserService.isAuthenticated; 
     $scope.user = UserService.getUserInfo; 
     $scope.login = UserService.login; 
    }]); 
+0

此代碼寫得很好,爲我過了指令,感謝您的詳細示例 – kimo

相關問題