简体   繁体   中英

What is the meaning of # inside the input tag?

I am going through an article of angular2 where they use <input type='text' #hobby> . With the help of #hobby they are extracting the value which are typing inside the textbox in place of taking ngModal. I am not getting that is #hobby means.

# , inside input, indicates that you are dealing with a local variable. Its an Angular2 way of defining a local variable in HTML. # will work behind the scene and updates your variable with relevant value.

<input type='text' #hobby>

Here hobby refers to a local variable as it is defined with # symbol. If you type in something in textbox, hobby variable will be updated immediately and you can access updated value by something.value syntax so hobby.value will give you updated value.

Try to add this to your template (html):

<button (click)="clicked(hobby)">Click</button>

This add to your component class:

clicked(a) {
  console.log(a);
}

You will see what that means.

See Template Syntax > Referencing a template reference variable .

The hash (#) prefix to "[hobby]" means that we're defining a [hobby] variable.

It is the same as using the ref-hobby attribute and allows you to access the content of the variable, for example in click handlers.

<input #hobby>
<button (click)="something(hobby.value)">Do Something</button>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM