Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use ng-model with label element.

Tags:

angularjs

I am reading a code and got into a part where a label element is been used with data-ng-model is that possible ?

<label class="btn btn-success"
  data-ng-model="myController.statusFilter"
  data-btn-radio="'disabled'"
  data-ng-click="myController.method()">
  Disabled
 </label>
like image 809
Petran Avatar asked Dec 23 '15 12:12

Petran


People also ask

How do you use NgModel labels?

Since ngModel should only be used with the inputs, since it involves two-way data binding. Label does not deal with user input, thus it does not require the ngModel. So if you want to bind a scope variable to the label then you can use expressions.

What is [( NgModel )] used for?

The ng-model directive binds the value of HTML controls (input, select, text-area) to application data. It is a part of the FormsModule. This directive is used by itself or as part of a larger form. It accepts a domain model as an optional Input.

Can we use NG-model for Div?

NgModel expects the bound element to have a value property, which div s don't have. That's why you get the No value accessor error. I don't know if the input event is supported on all browsers for contenteditable . You could always bind to some keyboard event instead.


3 Answers

It wont't work. Since ngModel should only be used with the inputs, since it involves two-way data binding.

Label does not deal with user input, thus it does not require the ngModel. So if you want to bind a scope variable to the label then you can use expressions.

Like

<label>  {{labelText}} </label>

Note : you should define labelText in your controller, like $scope.labelText = "Hello"

like image 149
Vivek Avatar answered Oct 14 '22 15:10

Vivek


Try to use ng-bind example in Plunker.

like image 32
George Kargakis Avatar answered Oct 14 '22 17:10

George Kargakis


<label class="btn btn-success"
  data-ng-bind="myController.statusFilter"
  data-btn-radio="'disabled'"
  data-ng-click="myController.method()">
  Disabled
 </label>

in this case ng-bind will work.

like image 23
Dhiraj Avatar answered Oct 14 '22 15:10

Dhiraj