我正在尝试使用bootstraps导航栏显示并从角度控制器中删除数据被告的html元素。
我有以下玉码:
div.navbar.navbar-fixed-top
div.navbar-inner
div.container-fluid(data-ng-controller="NavCtrl")
a.btn.btn-navbar(data-toggle='collapse', data-target='.nav-collapse')
span.icon-bar
span.icon-bar
span.icon-bar
div.nav-collapse.collapse
ul.nav
li
a(href='/topics') Topics
li(ng-show="curUser.admin")
a(href='/users') Users
li(ng-show="curUser.admin")
a(href='/organizations') Organizations
li(ng-show="curUser.admin")
a(href='/topicConfs') TopicConfig
li.divider
ul.nav.pull-right
{{authenticated}}
li.dropdown(ng-show="authenticated")
a.dropdown-toggle(role='button', data-toggle='dropdown', href='#') {{curUser.email}}
b.caret
ul.dropdown-menu(role='menu')
li
a(href='/users/{{curUser._id}}') Profile
li.divider
li
a.btn(ng-click="logout()") Logout
以及具有以下内容的控制器:
function NavCtrl($location, $scope, $rootScope, CurrentUser){
$scope.curUser = CurrentUser.getUser()
$scope.authenticated = CurrentUser.isAuthenticated()
$rootScope.$on('$routeChangeStart', function(){
$scope.curUser = CurrentUser.getUser()
$scope.authenticated = CurrentUser.isAuthenticated()
})
$scope.logout = function(){
CurrentUser.logout(function(result){
$scope.curUser = CurrentUser.getUser()
$scope.authenticated = CurrentUser.isAuthenticated()
console.log("authenticated before logout is %j", $scope.authenticated)
$location.url('/')
})
}
}
一切正常显示,直到$scope.authenticated
设置为false
且$scope.user
设置为{}
,其中没有任何ng-show属性在导航栏中更新
我需要做些什么才能让bootstraps nav元素响应$scope
变量的变化?
答案 0 :(得分:2)
当Angular $ scope属性在Angular的“外部”更改时,需要调用$scope.$apply()
以使Angular进入其digest loop。投射到当前视图的任何属性都将具有$ watch,摘要循环将对其进行评估。当在其中一个$ watch中检测到更改时,视图会更新。
Angular的“外部”示例:
element.bind('someEvent', function() {
//need to call scope.$apply in here
})
logout
。