Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between using id="mydiv" and #mydiv in Angular HTML?

Tags:

html

angular

It's a simple Angular HTML question, I would like a bit clarity about the id of tags. For example, in my code I have:

    <input class="inputfile" type="file" name="file" #file id="file" 
    (change)="onFileChange($event)"/>

    <button mat-mini-fab color="primary" (click)="file.click()">                        
            <mat-icon aria-label="Icon to upload file">cloud_upload</mat-icon>
    </button>

    <label for="file" >Upload your portifolio</label>

In that example, I had to set #file in the input for the button to work and also had to set id="file" for the label to work. Previously I thought they had the same function and it was just about syntax. Could someone clarify what are the uses of each?

like image 599
Gustavo Morais Avatar asked Feb 11 '19 15:02

Gustavo Morais


People also ask

Should I use div class or div id?

The simple difference between the two is that while a class can be used repeatedly on a page, an ID must only be used once per page. Therefore, it is appropriate to use an ID on the div element that is marking up the main content on the page, as there will only be one main content section.

What is the difference between id and class CSS?

The difference between an ID and a class is that an ID is only used to identify one single element in our HTML. IDs are only used when one element on the page should have a particular style applied to it. However, a class can be used to identify more than one HTML element.

What is div id used for?

The <div> tag defines a division or a section in an HTML document. The <div> tag is used as a container for HTML elements - which is then styled with CSS or manipulated with JavaScript. The <div> tag is easily styled by using the class or id attribute. Any sort of content can be put inside the <div> tag!

Why do we use ID in angular?

if you use just id you will have the error Cannot read property 'XXX' of undefined in your browser. For example, in order to show/hide a button using the value of an input in DOM file without writing any javascript/angular code you could do something like the following, in which setting the id wouldn't work out.


2 Answers

If you want to identify an element using javascript function or from your controller using getElementByID or to point to a style in a style sheet, you need to set the id that has to that element be unique throughout your DOM.

However, when you want to access your element within the DOM file you need to refer the element using #. if you use just id you will have the error Cannot read property 'XXX' of undefined in your browser.

For example, in order to show/hide a button using the value of an input in DOM file without writing any javascript/angular code you could do something like the following, in which setting the id wouldn't work out.

<form class="example-form">
  <mat-form-field class="example-full-width">
    <input #nameField matInput placeholder="Name">
  </mat-form-field>

  <button type="button" *ngIf="nameField.value!==''" >Submit</button>
</form>

Reference to this to dealing with user input and this as a broader explanation of the # tag.

like image 132
Thriller Avatar answered Oct 09 '22 14:10

Thriller


In an Angular application #mydiv can have different functions depending on the context.

  • On a DOM element <div #mydiv> is a reference to the element
  • A reference to an Angular component
  • On an element that is an Angular component, or has an Angular directive, where exportas:"ngform" is defined, #mydiv="ngForm" creates a component reference.
like image 33
MattHamer5 Avatar answered Oct 09 '22 13:10

MattHamer5