When to use the tag # in an input text?

2

I've seen that in angle 2 instead of using ngModel I can put something like this:

<input type="text" name="txtnombre" #Nombre />
<button (click) = "validarNombre(#Nombre)" > Validar </ button>

I can not understand well when this type of variable should be used with # and what is the advantage of using this instead of this

<input type="text" name="txtnombre" [(ngModel)]="nombre" />

Could someone explain to me? thanks.

    
asked by Panchin 17.04.2018 в 15:05
source

1 answer

3

The expression #nombre on an html element creates the reference on the element. For example when you do:

<button (click) = "validarNombre(#Nombre)" > Validar </ button>

The reference of the html element called #Nombre is being sent, in this case the input.

Now, a valid reason why you might want to do this is to minimize the use of angular bindings if it is not necessary. If you use in ngModel , angular you would have to do the binding to observe the changes in input but with the reference of the element that is not necessary. However, it is not good to carry out modification operations on an element with its reference unless it is necessary since it has a wide variety of directives for that.

    
answered by 17.04.2018 / 15:12
source