Issue
While taking an angular course I read the following code:
<p *ngIf="serverCreated;else noServer">{{serverName}} {{serverCreationStatus}}</p>
<ng-template #noServer>
<p>No server was created</p>
</ng-template>
I wonder if the expression #noServer is equivalent to the expression id="noServer". Is this way of doing specific to Angular? Is it recommended to use this way of defining the id?
Solution
The #noServer is what's called a template variable. It's not the same as an id attribute. It allows Angular to reference the element it's placed on, either from within the template:
<div *ngIf="myCondition; else otherDiv"></div>
<div #otherDiv>
Or from TypeScript using @ViewChild, for example:
@ViewChild('otherDiv') otherDiv: ElementRef
For more information, see Angular docs https://angular.io/guide/template-reference-variables
Answered By - Will Alexander
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.