Is it possible to bind data to a label using ngModel
? I want to display data fetched from the database and display using labels and not input(textbox).
There are 3 options :
<label [value]="someValueExpression"/>
<label>{{ value}}</label>
when you are using form:
<label for="name">Name</label>
<input type="text" class="form-control" id="name" required>
ngModel
works only for form's controls, which get input from the user. You need to use {{ }}
syntax.
<label>{{ yourData }}</label>
One of the way to achieve two way data binding for the label control.
<label for="name">{{name}}</label>
in your ts
export class MyComponent {
name="Wick";
//one of the hrml controls calls this method
somebodyCalledMe(){
this.name="John Wick";
}
So now whenever the name property changes automatically the label's text will also change accordingly.
No, it's not possible. The ngModel
only works for form's input controls. The label is not a form control.
You can bind other properties with []
i.e. the property binding.
<label [value]="expression"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With