Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I need to specify both a name and ng-model on input in AngularJS?

Tags:

angularjs

I was just wondering why I have to specify both name and ng-model on an input element in a form to be able to actually use it correctly. For example, with the following:

  <form name='test'>
      <div>
          <input type='radio' 
              value='create-new'
              ng-model='toggle'
              required />

          <input type='radio' 
              value='use-existing'
              ng-model='toggle'
              required />
      </div>
      <div ng-switch='test.$invalid'>
          <div ng-switch-when='true'>Invalid!</div>
          <div ng-switch-when='false'>Valid!</div>
      </div>
  </form>

I would get the output Invalid! when I don't select a radio button - this is correct behaviour. However, the downside is that the only way I can access this model is through $scope.toggle - the element itself will not be referencable by name inside of $scope.test (the form container). $scope.test will contain the validation rules for toggle, but it will not have any way of letting you know that those validation rules are belonging to toggle as the name is not present.

If we were to switch it around put a name attribute on the input:

  <form name='test'>
      <div>
          <input type='radio' 
              value='create-new'
              name='toggle'
              required />

          <input type='radio' 
              value='use-existing'
              name='toggle'
              required />
      </div>
      <div ng-switch='test.$invalid'>
          <div ng-switch-when='true'>Invalid!</div>
          <div ng-switch-when='false'>Valid!</div>
      </div>
  </form>

Then our ng-switch at the bottom will always show Valid, even though I've mentioned that the input itself is required. In addition, toggle now shows up inside of $scope.test, so I can check the validity of $scope.test.toggle elsewhere (without requiring an inline attribute on that element).

If I combine both approaches and use both name and ng-model, then both results are combined and I get the result I would have expected - I can see $scope.test.toggle and the model itself is validating correctly.

My question is why is this the appropriate behaviour? It seems different to, say, jquery.validate.unobtrusive, where the name attribute is the bit that intrinsically ties the validation rules to the element.

like image 433
Dan Avatar asked Oct 06 '14 13:10

Dan


People also ask

What is the purpose of name in ngModel?

name: An alternative to setting the name attribute on the form control element. See the example for using NgModel as a standalone control. standalone: When set to true, the ngModel will not register itself with its parent form, and acts as if it's not in the form.

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

For AngularJS there is no difference between ng-app and data-ng-app or ng-controller and data-ng-controller or ng-model and data-ng-model because while compiling HTML page, AngularJS strips data- or x- prefix.

What is NG-model options in AngularJS?

The ng-model-options directive is used to control the binding of an HTML form element and a variable in the scope. You can specify that the binding should wait for a specific event to occur, or wait a specific number of milliseconds, and more, see the legal values listed in the parameter values below.


1 Answers

name attribute is used for angular validation, ng-model for binding,

if you are not going to validate with angular you don’t have to use name

if you are not binding then don’t use ng-model

both are necessary if you need to validate in client side with angular and need binding

like image 95
bto.rdz Avatar answered Sep 17 '22 12:09

bto.rdz