Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference/incompatibility between ng-model and ng-value?

As far as i understood ng-model sets the value for that particular element in which the model is been assigned. given that how is ng-value different from ng-model?

like image 487
Divya MV Avatar asked Feb 25 '15 11:02

Divya MV


People also ask

What is the difference between ng-model and data NG model?

There is no difference between ng-model and data-ng-model if you see in terms of AngularJs. Actually, 'data' used as prefix to validate HTML5 validation. So, it is good practice to use data-ng-model , however, you can use ng-model as well. There is no problem in that also.

What is difference between value and ngModel in angular?

The NgModel class has the update property with an EventEmitter instance bound to it. This means we can't use (ngModelChange) without ngModel . Whereas the (change) event can be used anywhere, and I've demonstrated that above with the [value] property binding instead.

What is difference between ng-model and Ng bind?

ngModel usually use for input tags for bind a variable that we can change variable from controller and html page but ngBind use for display a variable in html page and we can change variable just from controller and html just show variable.

What is NG-model and Ng change?

Ng-change is a directive in AngularJS which is meant for performing operations when a component value or event is changed. In other words, ng-change directive tells AngularJS what to do when the value of an HTML element changes. An ng-model directive is required by the ng-change directive.


2 Answers

It works in conjunction with ng-model; for radios and selects, it is the value that is set to the ng-model when that item is selected. Use it as an alternative to the 'value' attribute of the element, which will always store a string value to the associated ng-model.

In the context of radio buttons, it allows you to use non-string values. For instance, if you have the radio buttons 'Yes' and 'No' (or equivalent) with values 'true' and 'false' - if you use 'value', the values stored into your ng-model will become strings. If you use 'ng-value', they will remain booleans.

In the context of a select element, however, note that the ng-value will still always be treated as a string. To set non-string values for a select, use ngOptions.

like image 139
JcT Avatar answered Sep 17 '22 14:09

JcT


Simple Description

ng-model

  1. Is used for two way binding of variable that can be available on scope as well as on html.
  2. which has $modelValue(value reside in actual scope) & $viewValue (value displayed on view).
  3. If you mentioned on form with name attribute then angular internally creates validation attributes for it like $error, $valid, $invalid etc.

Eg.

<input type="text/checkbox/radio/number/tel/email/url" ng-model="test"/> 

ng-value

  1. Is used to assign value to respective ng-model value like input, select & textarea(same can be done by using ng-init)
  2. ng-value does provide good binding if your are setting ng-model value through ajax while writing value attribute doesn't support it
  3. Basically meant to use for radio & option tag while creating select options dynamically.
  4. It can only have string value it, it doesn't support object value.

HTML

<input   [ng-value="string"]> ... </input> 

OR

<select ng-model="selected">   <option ng-value="option.value" ng-repeat="option in options">      {{option.name}}   </option> </select> 

...

like image 22
Pankaj Parkar Avatar answered Sep 19 '22 14:09

Pankaj Parkar