Ionic 2 : Radio button in form, checked by default - html

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>

Related

How to use multiple autofocus attribute in different forms of same component in Angular on ionic elements?

Here Autofocus is works only for Login page not working for Signup page.Also i want to fix autofocus on anther screens of same component And all bellow code is present in a single login.ts file.
Login HTML
<label class="ci-ter-head ci-opc-50">Email address</label>
</div>
<div class="wrapper ci-pl-30 ci-pr-30 ci-pt-5">
<ion-input autofocus="true" type="text" id="loginField" class="animated ci-form-field ci-font-size-12 ci-text-clr3 ci-back-input-colr ci-back-input-border-colr ci-mb-10 ci-email-height" [(ngModel)]="userSignInInfo.email" required></ion-input>
</div>
<div class="wrapper ci-pl-30 ci-mt-20">
<label class="ci-ter-head ci-opc-50">Password</label>
</div>
<div class="wrapper ci-pl-30 ci-pr-30 " style="position:relative;">
<ion-input type="{{feildType}}" clearOnEdit="false" id="passwordField" class="animated ci-form-field ci-font-size-12 ci-text-clr3 ci-back-input-colr ci-back-input-border-colr ci-mb-10 ci-password-height" [(ngModel)]="userSignInInfo.password" required>
<span *ngIf="!platform.is('ios')" [hidden] = "shPassword" (click)="showPassword($event);"><img src="../../assets/images/show.svg"></span>
<span *ngIf="!platform.is('ios')" [hidden] = "!shPassword" (click)="showPassword($event);"><img src="../../assets/images/hide.svg"></span>
</ion-input>
Signup HTML
<ion-col size-xs="12" size-md="6" class="ci-pr-20">
<label class="ci-ter-head ci-opc-50 ci-required">Email Address</label>
<ion-input autofocus="true" type="email" class="inputField" (ionChange)="emailKeydown($event, 'signUpForm')" [ngClass]="{'close-icon': validationVaribles.errorIconEmail, 'right-icon' : validationVaribles.successIconEmail }" [(ngModel)]="userSignUpInfo.email" class="ci-form-field ci-sec-head"></ion-input>
</ion-col>
<ion-col size-xs="12" size-md="6" class="ci-pr-20">
<label class="ci-ter-head ci-opc-50 ci-required">Screen Name</label>
<ion-input type="text" class="inputField" (ionChange)="userKeydown($event)" [ngClass]="{'close-icon': validationVaribles.errorIconUser, 'right-icon' : validationVaribles.successIconUser }" [(ngModel)]="userSignUpInfo.screenName" class="ci-form-field ci-sec-head"></ion-input>
</ion-col>
According to https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input
"No more than one element in the document may have the autofocus attribute. If put on more than one element, the first one with the attribute receives focus."
So you may try to bind a variable to your autofocus attribute and set it dynamically, something like this:
Input 1:
<ion-input [autofocus]="inputOneAutofocusEnabled">
Input 2:
<ion-input [autofocus]="inputTwoAutofocusEnabled">
and set inputOneAutofocusEnabled / inputOneAutofocusEnabled = true / false in your ts file.

(dblclick) has no effect

I'm writing a todolist demo.I try to double click to edit the item ,but it has no effect and no error.
the code as below
<li *ngFor="let todo of todos; let i = index;"
[ngClass]="{
completed: todo.isDone,
editing: currentEditing === todo
}"
>
<div class="view">
<input class="toggle" type="checkbox" [(ngModel)]="todo.isDone" >
<label (dblclick)="currentEditing = todo"> {{ todo.title }}</label>
<button class="destroy" (click)="delete(i)"> </button>
</div>
<input class="edit" value="Create a TodoMVC template">
</li>
but when i use (click),the item can editing.
<label (click)="currentEditing = todo"> {{ todo.title }}</label>
Can anyone see what I'm doing wrong? thanks!
Hi here is a link given below where dblclick work properly both for button and label on stackblitz. However, it works in different way as you have tried already. Please check: https://stackblitz.com/edit/angular-double-click-bndehs?file=src%2Fapp%2Fapp.component.html
Note: Please Check DoubleClickDirective class inside double-click.directive.ts file and #HostListener

Angular handling conditional driven form

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 ๐ŸŒŸ๐ŸŒŸ

Enabling input fields with radio button in ionic2

I am new to ionic 2 and I could not find a proper solution for this.I have two radio buttons in my signup form. I need to enable two in put fields based on the radio button that is clicked. If radio button one is clicked input field one should be enabled.
Radio buttons
<div radio-group formGroupName="church">
<label>Church</label>
<div>
<label>CGT</label>
<ion-radio value="cgt" ></ion-radio>
<label>Other church</label>
<ion-radio value="noncgt"></ion-radio>
</div>
</div>
<div>
Input fields
<div>
<label>CGT member ID </label>
<input formControlName="cgtMemberID" type="text" disabled="true">
<small [hidden]="myForm.controls.cgtMemberID.valid || (myForm.controls.cgtMemberID.pristine && !submitted)">
member ID required .
</small>
</div>
<div>
<label>Church Name </label>
<input formControlName="churchName" type="text" disabled="true">
<small [hidden]="myForm.controls.churchName.valid || (myForm.controls.churchName.pristine && !submitted)">
Church name required .
</small>
</div>
I think you need this:
1- in html:
<div radio-group>
<label>Church</label>
<div>
<label>CGT</label>
<ion-radio value="cgt" (ionSelect)="radio_select('cgt')"></ion-radio>
<label>Other church</label>
<ion-radio value="noncgt" (ionSelect)="radio_select('noncgt')"></ion-radio>
</div>
</div>
<div>
<label>CGT member ID </label>
<input type="text" [disabled]="cgt">
</div>
<div>
<label>Church Name </label>
<input type="text" [disabled]="cn">
</div>
2- and in .ts file:
cgt: boolean = true;
cn: boolean = true;
radio_select(value) {
if (value == 'cgt') {
this.cgt = false;
this.cn = true;
} else if (value == 'noncgt') {
this.cgt = true;
this.cn = false;
}
}
you also can add your form logic.

How to pre-select and update checkboxes in Angular 2+

I want to implement a checklist in Angular. Some boxes have to be pre-checked based on an array in the ts file and the array has to be updated when a checkbox is checked. I am doing it like:
<div class='form-group que-box' *ngFor="let option of currentQuestion.options">
<input type="checkbox" aria-label="Checkbox 1" [value]="option.text" name="checkBox" [(ngModel)]="optionsSelectedArray"/>
<label for='{{option.optionId}}'><span class="que">{{option.text}}</span></label>
</div>
optionsSelectedArray contains the option IDs retrieved from server.
But it doesn't work. All the checkboxes come checked when the the division is rendered.
EDIT
I have changed my code as follows :
<div class='form-group que-box' *ngFor="let option of currentQuestion.options">
<input type="checkbox" id="{{option.optionId}}" name="checkBox" [(ngModel)]="option.userResponse" (ngModelChange)= "setResponseChanged()">
<label for='{{option.optionId}}'><span class="que">{{option.text}}</span></label>
</div>
Here option.userResponse in boolean. Now on selecting the checkboxes the currentQuestion object is correctly changing but when the checklist loads for the first time, the checkboxes are not checked correctly.All the checkboxes come checked if the option.userResponse is true for the last item ,none come checked otherwise.
Perhaps this might do what you want
<div class='form-group que-box' *ngFor="let option of currentQuestion.options; let i=index">
<input type="checkbox" aria-label="Checkbox 1" name="checkBox" [(ngModel)]="optionsSelectedArray[i]"/>
<label for='{{option.optionId}}'><span class="que">{{option.text}}</span></label>
</div>
(I added ; let i=index and [i])