Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is #myId the same as id="myId" in HTML?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading