MEAN堆栈身份验证 - html锚标记href空链接覆盖ng-click功能重定向到基本href页面

时间:2017-09-22 03:59:15

标签: javascript html angularjs mean-stack

我收集了几种可能的解决方案,但它们都不适用于我。有人可以告诉哪个部分有错误。

这是我正在处理的注销功能,如果用户点击注销并返回登录页面,图标应该消失,但现在它只会导致登录页面。图标仍然存在,因为该功能没有触发。 只有在我点击登录页面中的按钮

后,一切正常

navigation.service.js

(function() {

angular
    .module('TheApp')
    .service('authentication', ['$window', function($window) {

        var saveToken = function(token) {
            $window.localStorage['auth-token'] = token;
        }

        var getToken = function() {
            return $window.localStorage['auth-token'];
        }

        var logout = function() {
            $window.localStorage.removeItem('auth-token');
            //delete $window.localStorage['auth-token'];
            //$window.localStorage.clear();
        }

        var isLoggedIn = function() {
            var token = getToken();
            //Get token from storage If token exists get payload, decode it, and parse it to JSON
            if (token) {
                let payload = JSON.parse($window.atob(token.split('.')[1]));
                return payload.exp > Date.now() / 1000;
            } else {
                return false;
            }
        }


        return {
            saveToken: saveToken,
            getToken: getToken,
            isLoggedIn: isLoggedIn,
            logout: logout
        };

    }])})();

navigation.directive.js

(function() {
angular
    .module('TheApp')
    .directive('navigation', navigation);

function navigation() {
    return {
        restrict: 'EA',
        templateUrl: './common/directives/navigation/navigation.template.html',
        controller: 'navigationCtrl as navvm'
    };
}})();

navigation.controller.js

(function() {
angular
  .module('TheApp')
  .controller('navigationCtrl', ['$location', 'authentication', '$window', function($location, authentication, $window) {
      console.log("enters navigation controller");
      var vm = this;

      vm.isLoggedIn = authentication.isLoggedIn();
      vm.logout = function() {
        console.log('test if the link work');
        authentication.logout();
        $location.path('/login');
      };
  }]);})();

navigation.template.html

<ul class="nav navbar-nav navbar-right">
    <li ng-show="navvm.isLoggedIn">
    <a href="" ng-click="navvm.logout()">
    <span aria-hidden="true" class="glyphicon glyphicon-log-out"></span></a></li>
</ul>

感谢。

溶液(UPDATE):

一个不小心的情况,我省略将控制器链接放在index.html(主模板页面)中。因此,如果有人在处理类似项目,那么上面的代码应该没有错误。

0 个答案:

没有答案