0

I'm wrapping my head around forms and ngModel currently. So far everything works great except for the radio buttons. They are loaded and all but I can't manage to read the touched state. I guess it is because they are generated within a for loop and therefore can't be referenced

<fieldset [ngClass]="{'has-error': someReference.touched 
                                   && someReference.invalid}">
    <legend>Salutation</legend>
    <div>
        <label for="salutation_m">
            <input id="salutation_m" name="salutation" type="radio" 
                   [ngModel]="registration.salutation" #someReference="ngModel">
            <span>Mister</span>
        </label>
    </div>
</fieldset>

So far it looks like the reference can't be read. I don't get any errors but even when the element is touched and invalid the class is not set.

Any hints on what I'm doing wrong?

Update

Changed #someReference to #someReference="ngModel" which solves the problem for predefined radio buttons but not for generated ones inside of a for loop.

I'm guessing in the case of a for loop there is a problem with the scope.

Chuvisco
  • 101
  • 1
  • 6
  • http://stackoverflow.com/questions/31879497/angular2-radio-button-binding – anshuVersatile Jan 03 '17 at 10:37
  • Thanks for pointing out, but it isn't really helpful. Like I said, the radios are loaded and working except of the class not being set. So far I figured, that I forgot to add ngModel after someReference like `#someReference="ngModel"` which works for predefined radio buttons, but not for radio buttons generated inside a for loop. Guess that is a different problem because of the scopes... – Chuvisco Jan 03 '17 at 10:48

0 Answers0