Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between @bind and @bind-value

What is the difference of using @bind and @bind-value?

I made this simple example, and testing it in the browser, I didn't see any difference.

<p>@@bind @increment1</p>  <input      type="text"     @bind="@increment1" />  <p>@@bind-value @increment2</p> <input      type="text"     @bind-value="@increment2" />  @code {     string increment1;     string increment2; } 
like image 283
Vencovsky Avatar asked Oct 03 '19 15:10

Vencovsky


People also ask

What is the difference between bind and bind value?

In Blazor, there is no significant difference between using these two attributes. The @bind attribute is a shorthand of the @bind-value attribute and its delegate will trigger the ValueChanged event of the component.

What is a bind value?

Bind parameters—also called dynamic parameters or bind variables—are an alternative way to pass data to the database. Instead of putting the values directly into the SQL statement, you just use a placeholder like ? , :name or @name and provide the actual values using a separate API call.

What is bind value in Blazor?

By default, binding applies to the element's onchange event. If the user updates the value of the text box's entry to 123.45 and changes the focus, the element's value is reverted to 123 when onchange fires.

How do you bind a label to value in Blazor?

You can use the bind attribute on any element to bind the value. In blazor, we'll have a property assigned some value in the functions and use the property in the HTML template. Let's get this done. So, when we run the app, the label tag will display “red” as a text in the label.


2 Answers

Short Version

@bind is an override of @bind-value with the event set to "onchange".

These two commands are equivalent:

 ... @bind-value="userName" @bind-value:event="onchange" ...  ... @bind="userName" ... 

Long Version

The @bind attribute accomplishes two separate (but related) tasks:

  1. Binds an expression to the value of the <Input... component
  2. Binds a delegate that will trigger the component's ValueChanged property

Both the expression and the delegate are required. An implementation of @bind-Value looks like this:

 ... @bind-value="userName" @bind-value:event="onchange" ... 

We are setting both the expression (="userName") and the delegate (="onchange").

The "easier" @bind= is just an override with the delegate preset to "onchange". So these two commands are functionally the same:

 ... @bind-value="userName" @bind-value:event="onchange" ...  ... @bind="userName" ... 

A greatly simplified analogy using overriding methods:

public void bind-value(string value, string event) {..}  public void bind(string value) {   bind-value(value, "onchange"); } 

A couple of common use-cases for using the full @bind-value version are

  1. updating the UI as the user types
  2. validating an email address as the user types

Remember, the onchange event will only trigger PropertyChanged when the component loses focus. Instead, we want PropertyChanged to be triggered by the oninput event:

... @bind-value="H1Content" @bind-value:event="oninput" ... ... @bind-value="email" @bind-value:event="oninput" ... 
like image 95
Shai Cohen Avatar answered Oct 02 '22 10:10

Shai Cohen


Quoting Component Binding docs:

Data binding to both components and DOM elements is accomplished with the @bind attribute. (...) Using @bind with a CurrentValue property (<input @bind="CurrentValue" />) is essentially equivalent to the following:

<input value="@CurrentValue"        @onchange="@((ChangeEventArgs __e) => CurrentValue = __e.Value)" /> 

In addition to handling onchange events with @bind syntax, a property or field can be bound using other events by specifying an @bind-value attribute with an event parameter (@bind-value:event). ( onchange, oninput )

Summarizing

If you want to reset binded value on each input change (instead to set all changes at once on lost input focus) you should to use @bind-value and oninput on @bind-value:event:

<input @bind-value="CurrentValue"         @bind-value:event="oninput" /> 

If you try to use @bind-value:event without @bind-value (using just @bind ) a pre-compiling error is raised:

error RZ10004: Could not find the non-parameterized bind attribute that corresponds to the attribute 'bind-value:event'

But the XXX.razor.g.cs generated file is the same for @bind and @bind-value.

like image 27
dani herrera Avatar answered Oct 02 '22 09:10

dani herrera