Angular handling conditional driven form - html

I am very new to Angular, I am using template driven form.
I have two questions in a form as radio buttons (shown below) , My use case is that, Once user answers both these questions, based on his/her response, I need to show another set of questions.
So, if answer to any of the 2 question is "yes", then I need to show another set of question.If answer to both question is "no", then I need not show another set of question. Pls help how to achieve it.
My template and component.ts file are shown below. I tried to use "*ngIf" with condition
*ngIf="Question1 || Question2", but this doesn't work because as soon as the first question is answered "yes", it displays section with text " Show rest questions". Please see my sample code below. I want that when user answers both the questions, then only the div with text "Show rest questions" or "Don't Show rest questions" should be displayed, based on the response.
<div class="form-group">
<div class="form-group col-md-12">
<label>Question 1?</label>
<input type="radio" name="Question1" [(ngModel)]="Question1" value="yes">Yes
<input type="radio" name="Question1" [(ngModel)]="Question1" value="no">No
</div>
<div class="form-group col-md-12">
<label>Question 2?</label>
<input type="radio" name="Question2" [(ngModel)]="Question2" value="yes">Yes
<input type="radio" name="Question2" [(ngModel)]="Question2" value="no">No
</div>
</div>
<div align="center" *ngIf="Question1 || Question2">
Show rest questions
</div>
<div align="center" *ngIf="Question1 && Question2">
Don't show rest questions
</div>
Below is my partial component.ts
export class Component1 implements OnInit {
public Question1:string;
public Question2:string;
}

this value="yes" will set the selected value to a string , if you want to set the value to true / false try like this
<div class="form-group">
<div class="form-group col-md-12">
<label>Question 1?</label>
<input type="radio" name="Question1" [(ngModel)]="Question1" [value]="true">Yes
<input type="radio" name="Question1" [(ngModel)]="Question1" [value]="false">No
</div>
<div class="form-group col-md-12">
<label>Question 2?</label>
<input type="radio" name="Question2" [(ngModel)]="Question2" [value]="true">Yes
<input type="radio" name="Question2" [(ngModel)]="Question2" [value]="false">No
</div>
</div>
then you can use ngIf to show another section like this
<div align="center" *ngIf="Question1 || Question2; else noQuestions ">
Show rest questions
</div>
<ng-template #noQuestions>
.....
</ng-template>
Question1 || Question2 will become true of any of the answer is selected yes , or you can show the rest in both of the answer is yes like this Question1 && Question2
demo ๐Ÿš€๐Ÿš€
updated!! โšกโšก
set the value to true/false will limt our checking of the selected cases
<div class="form-group">
<div class="form-group col-md-12">
<label>Question 1?</label>
<input type="radio" name="Question1" [(ngModel)]="Question1" value="yes">Yes
<input type="radio" name="Question1" [(ngModel)]="Question1" value="no">No
</div>
<div class="form-group col-md-12">
<label>Question 2?</label>
<input type="radio" name="Question2" [(ngModel)]="Question2" value="yes">Yes
<input type="radio" name="Question2" [(ngModel)]="Question2" value="no">No
</div>
</div>
<div *ngIf="Question1 === 'yes' && Question2 === 'yes' else both">
both questions is selected as yes
</div>
<ng-template #both>
<ng-container *ngIf="Question1 === 'yes' || Question2 === 'yes' else oneAsNo">
one of the questions is selected as yes
</ng-container>
</ng-template>
<ng-template #oneAsNo>
<ng-container *ngIf="Question1 === 'no' || Question2 === 'no' else none">
one of the questions is selected as no
</ng-container>
</ng-template>
<ng-template #none>
none of the questions
</ng-template>
demo ๐ŸŒŸ๐ŸŒŸ

Related

Angular Checkbox Value to Typescript

i'm trying to make a quiz where there is a card and 4 questions.
I have the 4 questions Like this
<div class="col-md-6">
<div class="option" id="Answer1">
<label class="Answer1">
<input value= "Answer1"
type="checkbox"
(change) ="CheckAnswer(value)"/>{{question.answer1}}
</label>
</div>
<div class="option" id="Answer2">
<label class="Answer2">
<input value= Answer2
type="checkbox"
(change) ="CheckAnswer(value)"/>{{question.answer2}}
</label>
</div>
<div class="option" id="Answer3">
<label class="Answer3">
<input value= "Answer3"
name="Answer3"
type="checkbox"
(change) ="CheckAnswer(value)"/>{{question.answer3}}
</label>
</div>
<div class="option" id="Answer4">
<label class="Answer4">
<input value= "Answer4"
type="checkbox"
(change) ="CheckAnswer(value);"/>{{question.answer4}}
</label>
</div>
</div>
But now i am trying to give the value of the one that has been pressed to my TS file with the method CheckAnswer(value).
This is my method in TS to test if the value is given . In my console it keeps saying value is undefined
CheckAnswer(value : string) {
console.log(value);
}
You're getting undefined because value is undefined in CheckAnswer(value) in the HTML.
Try this for all your inputs:
<input value= "Answer4"
type="checkbox"
(change)="CheckAnswer($event);"/>{{question.answer4}}
And the method:
CheckAnswer(event : any) {
console.log(event); // should log something now
console.log(event.target.value); // should be the value of the checkbox
}

For loop - bind dynamic key to the #id

I have a for loop that displays the data into multiple radio-buttons (bootstrap):
<div class="radio" >
<label v-for="(choice, index) in choices" v-if="choice.question_id == question.id" :key="choice.id">
<input type="radio" name="optradio" id="choice.id"> [[choice.content]]
</label>
</div>
As you can see, I wanted to use the choice.id for id="..." in each button, technically it should look something like this:
<input type="radio" name="optradio" id="1"> Choice1
<input type="radio" name="optradio" id="2"> Choice2
<input type="radio" name="optradio" id="3"> Choice3
But it renders it with the actual string choice.id:
<input type="radio" name="optradio" id="choice.id"> Choice1
<input type="radio" name="optradio" id="choice.id"> Choice2
<input type="radio" name="optradio" id="choice.id"> Choice3
Forgive my naiveness. Any help/advices? Thanks a lot!
It renders with choice-id string because you add plain string as the id value, not a variable value
You can use v-bind directive or the shorthand for v-bind -> :id
<div class="radio" >
<label v-for="(choice, index) in choices" v-if="choice.question_id == question.id" :key="choice.id">
<input type="radio" name="optradio" v-bind:id="choice.id"> [[choice.content]]
</label>
</div>
using shorthand <input type="radio" name="optradio" :id="choice.id">
To answer your questions in the comments.
You can ' separate 'the radios by adding them in a ' group ' using the name attribute. Radios with the same name attribute are in one group. Changing their values won't affect other radios in other groups ( with different name attributes ). See example below.
Or you can use vue v-model to separate and get the selected options.
new Vue({
el: "#radio-app",
data: {
question1: '',
question2: ''
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://github.com/vuejs/vue-devtools"></script>
Question1:
<input type="radio" name="question1" id="q1choice1" value="choice1"/>
<input type="radio" name="question1" id="q1choice2" value="choice2"/>
Question2:
<input type="radio" name="question2" id="q2choice1" value="choice1"/>
<input type="radio" name="question2" id="q2choice2" value="choice2"/>
<hr />
<h2> Or with VUE v-model </h2>
<div id="radio-app">
Question1:
<input type="radio" id="q1choice1vue" value="choice1" v-model="question1">
<input type="radio" id="q1choice2vue" value="choice2" v-model="question1">
Question2:
<input type="radio" id="q2choice1vue" value="choice1" v-model="question2">
<input type="radio" id="q2choice2vue" value="choice2" v-model="question2">
<div>Question 1 selected answer: {{ question1 }}</div>
<div>Question 2 selected answer: {{ question2 }}</div>
</div>
Check more here
VUE template syntax
v-bind directive
Use: v-bind:id="choice.id" or the short version :id="choice.id"

Angular 2 checkbox validation

I am trying to validate checkbox in angular 2 by template driven but not working.I have searched in google also no one answered properly.Any genius can answer this question?
https://stackblitz.com/edit/angular-9nkywb?file=src%2Fapp%2Fapp.component.html
app.component.html
<form #f="ngForm">
<input type="checkbox" name="isTCAccepted" [ngModel]="user" required
#tc="ngModel">Name1
<input type="checkbox" name="isTCAccepted" [ngModel]="user" required
#tc="ngModel">Name2
<input type="checkbox" name="isTCAccepted" [ngModel]="user" required
#tc="ngModel">Name3
<div *ngIf="tc.invalid && f.submitted">
Please check atleast one
</div>
<button>Submit</button>
</form>
As I see, Everything is fine with the code except that name for the input tags should be different, you can have a look into below code,
`
<form #f="ngForm">
<input type="checkbox" name="isTCAccepted1" [ngModel]="user" required
#tc1="ngModel">Name1
<input type="checkbox" name="isTCAccepted2" [ngModel]="user" required
#tc2="ngModel">Name2
<input type="checkbox" name="isTCAccepted3" [ngModel]="user" required
#tc3="ngModel">Name3
<div *ngIf="(tc1.invalid && tc2.invalid && tc3.invalid) && f.submitted">
Please check atleast one
</div>
<button>Submit</button>
</form>
`

Distinguishing checkboxes with the same ID in different classes

Hope you can help me regarding webscraping via VBA. In a webform I have to check and uncheck checkboxes using VBA. Now I run into the difficulty that some checkboxes have the same ID but reside in different classes.
Example:
Class "filter" has a checkbox with ID "start_date" and class "return_fields" has a checkbox with ID "start_date". When using .getElementByID("start_date") both checkboxes switch simultaneously.
But in this case I want to have the checkbox in class "filter" to be checked and the one in "return_fields" to be unchecked.
So I've attempted something like
.getElementsByClassName("filter")(0).getElementById("start_date").checked = True
.getElementsByClassName("return_fields")(0).getElementById("start_date").checked = False
Unfortunately this returns an error "Object doesn't support this property or method".
Here is the content of the element I get with .getElementByClassName("filter"):
<legend>Date Mode</legend>
<div class="start_date required">
<label for="active_date">Start Date</label>
<input name="date_mode" class="radio" id="start_date" type="radio" checked="checked" value="start_date">
</div>
<div class="report_date required">
<label for="report_date">Report Date</label>
<input name="date_mode" class="radio" id="report_date" type="radio" value="report_date">
</div>
<div class="end_date required">
<label for="end_date">End Date</label>
<input name="date_mode" class="radio" id="end_date" type="radio" value="end_date">
</div>

Ionic 2 : Radio button in form, checked by default

I try to create a dynamic form in my project and I have some radio button. I want to check one of them by default. I tried this :
<input *ngIf="fieldForm.value.type == 'date'" type="date" formControlName="field">
<div *ngIf="fieldForm.value.type == 'radio'">
<div *ngFor="let option of fieldForm.value.options.values" >
<label><input type="radio" formControlName="field" value="{{option}}">{{option}}</label>
</div>
<div>
<label><input type="radio" formControlName="field" value="Toto" checked="checked">Toto</label>
</div>
</div>
But it doesn't work. I don't understand what I have to do. Someone have an idea ?
It's ok ! I have find a solution !
In my html :
<div *ngIf="fieldForm.value.type == 'radio'">
<ion-list formControlName="field" radio-group [(ngModel)]="value">
<ion-item *ngFor="let option of fieldForm.value.options.values" >
<ion-label>{{option.label}}</ion-label>
<ion-radio value="{{option.value}}"></ion-radio>
</ion-item>
</ion-list>
</div>
The "ngModel" permit to get selected value. I have to init it with a correct value in my .ts file.
easy all you need is just to delete this: checked="checked"
so your code will look like:
<input *ngIf="fieldForm.value.type == 'date'" type="date" formControlName="field">
<div *ngIf="fieldForm.value.type == 'radio'">
<div *ngFor="let option of fieldForm.value.options.values" >
<label><input type="radio" formControlName="field" value="{{option}}">{{option}}</label>
</div>
<div>
<label><input type="radio" formControlName="field" value="Toto">Toto</label>
</div>
</div>