在我的 angularjs 应用程序中,我正在为每个页面呈现我的“导航栏”div。
用户登录并重定向到详细信息页面后,我希望我正在更新 $scope ,它没有反映到视图中。
为了反映 $scope 的变化,我使用 $scope.$apply() 调用 $digest。似乎它没有更新,并且 $scope 更新仍然没有反映在我的观点中。
我的代码如下所示:
CONTROLLER:
function NavCtrl($scope){
//as isAuth false
$scope.showLogin = true;
$scope.showLogout = false;
}
function ProductDetails($scope){
//as isAuth true
$scope.showLogin = false;
$scope.showLogout = true;
if (!$scope.$$phase) {
//$digest or $apply to reflect update of scope update
$scope.$apply();
}
}
VIEW:
<div id="navigation-bar" ng-controller="NavCtrl">
<li ng-show="showLogin"><a href="/login">Login</a></li>
<li ng-show="showLogout"><a href="/logout">Logout</a></li>
</div>
我做错了什么?我错过了什么吗?顺便说一句,我遇到了其他问题,例如AngularJS $scope 更新未反映在视图中,但对我的情况没有帮助。