尝试使用自定义指令基于用户登录显示隐藏菜单.这一切在视图模板中工作正常,但在标题中它不起作用[标题是一个不同的模板].它在页面刷新时工作正常.
了header.html
has-logged指令
angular.module('myApp') .directive('hasLogged', function(CookieService) { return { link: function(scope, element, attrs) { if(!_.isString(attrs.hasLogged)) throw "hasLogged value must be a string"; var value = attrs.hasLogged.trim(); var notLoggedFlag = value[0] === '!'; if(notLoggedFlag) { value = value.slice(1).trim(); } function toggleVisibilityBasedOnLogin() { var logged = CookieService.getLoginStatus(); if(logged && !notLoggedFlag || !logged && notLoggedFlag) element.show(); else element.hide(); } toggleVisibilityBasedOnLogin(); } }; });
app.js配置
var myApp = angular.module('myApp',['ngRoute','ngCookies']); myApp.config(function ($routeProvider,$httpProvider) { $routeProvider .when('/', { templateUrl: 'app/module/public/index.html', header: 'app/partials/header.html', footer: 'app/partials/footer.html' }) .when('/login', { templateUrl: 'app/module/login/login.html', header: 'app/partials/header.html', footer: 'app/partials/footer.html' }) .when('/home', { templateUrl: 'app/module/home/home.html', header: 'app/partials/header.html', footer: 'app/partials/footer.html' }) .when('/register', { templateUrl: 'app/module/register/register.html', header: 'app/partials/header.html', footer: 'app/partials/footer.html' }) .otherwise({redirectTo: '/'}); });
在app运行时添加页眉和页脚的代码
// Adds Header and Footer on route change success $rootScope.$on('$routeChangeSuccess', function (ev, current, prev) { $rootScope.flexyLayout = function(partialName) { return current.$$route[partialName] }; });
我试过这个POST解决方案,但效果仍然相同.如何在没有页面刷新的情况下更改菜单?