I'm trying to read the radio button value - in angular 2,
index.html
<input type="radio" id="options1" name="function" value="create">
<input type="radio" id="options2" name="function" value="continue">
index.ts
@Component({
selector: 'function',
templateUrl: './client/components/function/index.html',
styleUrls: ['./client/components/function/index.css'],
providers: [],
directives: [ROUTER_DIRECTIVES]
})
I need to display a div in the html based on if radio button value is create or continue.
What I've tried:
document.getElementById
- Property checked
wasn't getting detected.model.function
by defining a model
in the typescript. Unable to access the model
variable, obviously![(ngModel)]
- that didn't work either.Please suggest a turnaround for this.
For validating the Material radio button, we'll add a class dynamically to the radio button group. The class will be added once, the form is submitted and the radio button value is invalid or not selected. NgForm directive helps in detecting whether the form is submitted.
Value. The value attribute is a string containing the radio button's value. The value is never shown to the user by their user agent. Instead, it's used to identify which radio button in a group is selected.
template:
<input type="radio" id="options1" [(ngModel)]="myRadio" value="create">
<input type="radio" id="options2" [(ngModel)]="myRadio" value="continue">
then define a myRadio
variable in your component and initialize it like this:
myRadio: string = ''
this variable will get the chosen value.
and, do not use javascript to control DOM elements in Angular2, this is against the angular2 philosophy
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With