Forcing revaluation of ng-if

I have a headline in my application that contains the Login / Create Account option. When a user logs in, I need you to replace the Logout option.

However, when a user logs in to a ui-view, it changes as expected, but does not change the title, which is understandable because it is not part of the ui-view. Here is my code:

<ul id="nav-account">
    <li>{{mainCtrl.isAuthenticated ? 'Welcome ' + user.firstname + '!'</li>
    <li ng-if="!isAuthenticated"><a ng-href="{{'HEADER_NAVTABS_SIGNIN'| externallinks}}">Sign In</a> or <a ng-href="{{'HEADER_NAVTABS_CREATE_ACCOUNT'| externallinks}}">Create Account</a></li>
    <li ng-if="isAuthenticated"><a ui-sref="logout">Sign Out</a><li>
</ul>

In my controller, I set the value to isAuthenticated by calling the service:

 $scope.isAuthenticated = memberService.isAuthenticated();

The only way to change the title value is to refresh the entire page - why is this? Should ng-if not overestimate the variable and update the view accordingly?

Does this mean that ng-if is connected in only one way? So what can I do to make this work?

thank

Andrew

+4
2

. , .

, if ngIf. , , :

1. ( ) :

Angular:

$scope.isAuthenticated = memberService.isAuthenticated;

Partial:

<li>{{isAuthenticated() ? 'Welcome ' + user.firstname + '!'}}</li>

: angular isAuthenticated() .

-

2. , :

Angular:

$scope.auth = memberService.authenticationData;

Partial:

<li>{{auth.isAuthenticated ? 'Welcome ' + user.firstname + '!'}}</li>

-

, @Govan, ngShow, JS:

<li ng-show="auth.isAuthenticated">Welcome {{user.firstname}}!</li>
+2

, - , , angular. $apply, :

$scope.$apply(function() {
    $scope.isAuthenticated = memberService.isAuthenticated();
});

. $scope. $apply

-1

Source: https://habr.com/ru/post/1569693/


All Articles