I worked a few years in .NET, Silverlight now I'm starting with Angular 2 and Expressjs. And I have a doubt that even I could not find how can I do this in angular 2 + Expressjs, and is safe from client side?
<% if(User.Identity.IsAuthenticated){ %>
<b>Sign Out</b>
<% } else { %>
<b>Sign In</b>
<% } %>
We can use multiple conditions in *ngIf with logical operator AND (&&) to decide the trustworthy of *ngIf expression. If all conditions are true, then element will be added to the DOM.
The asterisk is "syntactic sugar". It simplifies ngIf and ngFor for both the writer and the reader. Under the hood, Angular replaces the asterisk version with a more verbose form.
Use ngFor and ngIf on same element It's very common scenario where we want to repeat a block of HTML using ngFor only when a particular condition is true. i.e., if ngIf is true.So in this case to use *ngIf and *ngFor on same element, place the *ngIf on a parent element that wraps the *ngFor element.
If you need the "else", perhaps the best way is to use NgSwitch (https://angular.io/docs/ts/latest/api/common/index/NgSwitch-directive.html)
So something like:
<div [ngSwitch]="User?.Identity?.IsAuthenticated">
<b *ngSwitchCase="true">Sign Out</b>
<b *ngSwitchDefault>Sign In</b>
</div>
With Angular 4, this is now possible:
SYNCHRONOUSLY
lets assume that we've defined this class variable:
shown: boolean = true;
If else syntax:
<button (click)="shown = !shown">toggle 'shown' variable</button>
<div *ngIf="shown; else elseBlock">If shown</div>
<ng-template #elseBlock>else not shown</ng-template>
Note that ng-template
must be used in order for this structural directive to work, so if you have a custom component, wrap it inside ng-template
. Below is an alternative syntax which also binds to the same class variable.
If then else syntax:
<button (click)="shown = !shown">toggle 'shown' variable</button>
<div *ngIf="shown; then primaryBlock; else secondaryBlock"></div>
<ng-template #primaryBlock>If shown</ng-template>
<ng-template #secondaryBlock>else not shown</ng-template>
ASYNCHRONOUSLY
class:
userObservable = new Subject<{first: string, last: string}>();
onButtonClick() {
this.userObservable.next({first: 'John', last: 'Smith'});
}
template:
<button (click)="onButtonClick()">Display User</button>
<div *ngIf="userObservable | async as user; else loading">
Hello {{user.last}}, {{user.first}}!
</div>
<ng-template #loading>Waiting for click...</ng-template>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With