Reputation: 891
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).
Upvotes: 9
Views: 49992
Reputation: 1
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"/>
Upvotes: 0
Reputation: 4265
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.
Upvotes: 2
Reputation: 5391
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>
Upvotes: 15
Reputation: 68655
ngModel
works only for form's controls, which get input from the user. You need to use {{ }}
syntax.
<label>{{ yourData }}</label>
Upvotes: 5