Build an <ion-list radio-group> with ngFor - ionic2

I'm trying to build an ion-list radio-group with *ngFor from an array. I have an array of games (id, name) and I want only a list with them where the user can choose only one, but local variables seems to be broken (the first should be checked but this does not work too). Here is my code:
<ion-header>
<ion-navbar>
<ion-title>
New match
</ion-title>
</ion-navbar>
</ion-header>
<ion-content>
<ion-list radio-group>
<ion-list-header>
Game
</ion-list-header>
<ion-item *ngFor="let game of games, let i = index">
<ion-label>{{ game.name }}</ion-label>
<ion-radio checked="i == 0" value="game.id"></ion-radio>
</ion-item>
</ion-list>
</ion-content>
What am I doing wrong? Anybody have an idea? Thanks.

You have to use double brackets {{}} around the checked="i==0" and the value="game.id" Like so:
<ion-radio checked="{{i == 0}}" value="{{game.id}}"></ion-radio>
Otherwise, the checked and value attributes evaluate the content as a strings, not expressions.

Related

Ionic2 selcted value undefined

Hi I have a select box in ionic 2 as.
//home.html
<ion-item>
<ion-label>Select City</ion-label>
<ion-select [(ngModel)]="city" (ionChange) ="getDoors()">
<ion-option *ngFor="let city of cities" [value]="city.id">
{{city.name}}</ion-option>
</ion-select>
</ion-item>
//home.ts
getDoors(){
console.log(this.cities);
}
But when I am changing option it throw an error, Error trying to diff '1'
can anyone let me know the issue here
Thanks advance
There are 2 methods through which you can get the value of the selected Item.
Method 1
//home.html
<ion-item>
<ion-label>Select City</ion-label>
<ion-select [(ngModel)]="city" (ionChange) ="getDoors($event)">
<ion-option *ngFor="let city of cities" [value]="city.id">
{{city.name}}</ion-option>
</ion-select>
</ion-item>
//home.ts
getDoors($event){
console.log($event);
}
Method 2
By using ngModel
//home.html
<ion-item>
<ion-label>Select City</ion-label>
<ion-select [(ngModel)]="selectedcity" (ionChange) ="getDoors($event)">
<ion-option *ngFor="let city of cities" [value]="city.id">
{{city.name}}</ion-option>
</ion-select>
</ion-item>
//home.ts
selectedcity: city
getSelectedCity(){
return this.selectedcity;
}
Perhaps it's because you named your ngModel city and your *ngFor uses city as well. Might cause a conflict. I would change your ngModel to
[(ngModel)]="selectedCity"

Ionic 2 ion-select dynamically generated. How do I clear the list of options?

I have a form in my Ionic 2 project that has 2 ion-select dropdowns. When the user selects an option in the 1st dropdown it generates the options of the 2nd dropdown. This works fine. My question is, if the user changes their selection for the 1st dropdown the selected 2nd option does not clear out. How do I get it to clear so the user is not confused about the options for the new select?
<ion-item>
<ion-label>
<img height="100" src="assets/img/customer.png" alt="Customer"/>
</ion-label>
<ion-select [(ngModel)]="customer" name="customer" (ionChange)="addBuildings(customer);" placeholder="Customer">
<ion-option *ngFor="let customer of customers" value="{{customer.name}}">{{customer.name}}</ion-option>
</ion-select>
</ion-item>
<ion-item>
<ion-label>
<img height="100" src="assets/img/building.png" alt="Building"/>
</ion-label>
<ion-select [(ngModel)]="building" name="building" placeholder="Building">
<ion-option *ngFor="let building of buildings" value="{{building.name}}">{{building.name}}</ion-option>
</ion-select>
</ion-item>
I solved this by using the ngModel and setting it to null in the .ts at the beginning of the .addBuildings() function.

Display inline label, input box and icon/button in ionic 2

I want to display label and input on left side sequentially and button/icon on right side.
i have tried with using grid as
<ion-grid>
<ion-row>
<ion-col width-10>
<ion-icon name="phone-portrait"></ion-icon>
</ion-col>
<ion-col width-70>
<ion-input type="text" placeholder="Mobile no"></ion-input>
</ion-col>
<ion-col width-10>
<ion-icon name="contacts" (click)="pickContactNo()"></ion-icon>
</ion-col>
</ion-row>
</ion-grid>
It display's it inline but the input box slightly goes below.
also tried with item-left and item-right properties. but cant able to do.
Try this:
<ion-item>
<ion-icon item-left name="phone-portrait"></ion-icon>
<ion-input type="text" placeholder="Mobile no"></ion-input>
<ion-icon item-right name="contacts" (click)="pickContactNo()"></ion-icon>
</ion-item>
You will probably have to override some of the css in the elements of ion-grid otherwise.
UPDATE From ionic#3.4.0 :
Thanks to #keldar for the comment. According to the docs,directional properties item-left and item--right are deprecated and you would need to use item-start and item-end which would allow to support both Right to Left and Left to Right.
<ion-item>
<ion-icon item-start name="phone-portrait"></ion-icon>
<ion-input type="text" placeholder="Mobile no"></ion-input>
<ion-icon item-end name="contacts" (click)="pickContactNo()"></ion-icon>
</ion-item>
ion-row has several attributes (see Row). The one that looks suitable for you is align-items-center.
So, you can try this:
<ion-grid>
<ion-row align-items-center>
...
if you add new col, it mean that will be another column and you don't have unspecified the dimensions of these
When you add
you can insert one ion-item to insert everything on the same line without static sizes.
<ion-item>
<ion-icon item-start name="phone-portrait"></ion-icon>
<ion-input type="text" placeholder="Mobile no"></ion-input>
<ion-icon item-end name="contacts" (click)="pickContactNo()"></ion-icon>
</ion-item>

How to prevent the keyboard from opening automatically in my Ionic2 App?

I am developing a Mobile App using the Ionic2-Framework along with Angular2.
In a section of my app, I have several <ion-slides> with <ion-item><ion-input ...></ion-input></ion-item> on the slides.
When changing the slide, the keyboard opens up without actively touching one of the input-fields. So far, I could only test it on iOS, but I found out that it only happens for type="tel"-inputs
Is there a way to prevent the app from doing this, as I would like the users to choose the time to input by themselves.
The code of my page looks like that:
(I excluded some parts for readability)
<ion-header>
<ion-navbar>
<button ion-button icon-only menuToggle [hidden]="showBackButton">
<ion-icon color="primary" name="menu"></ion-icon>
</button>
<button color="light" ion-button icon-only [hidden]="!showBackButton" (click)="goToPreviousSlide()">
<ion-icon color="primary" name="arrow-back"></ion-icon>
</button>
<ion-title>Title</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<ion-slides (ionSlideDidChange)="updateShowBackButton()">
<!-- Some more slides... -->
<ion-slide style="background-color: white" padding>
<h2 class="slide-title">Hello {{person.name}}!</h2>
<p>Now I need this and that information:/p>
<ion-item>
<ion-label>Day</ion-label>
<ion-input type="tel" fixed [(ngModel)]="person.day"></ion-input>
</ion-item>
<ion-item>
<ion-label>Month</ion-label>
<ion-input type="tel" fixed [(ngModel)]="person.month"></ion-input>
</ion-item>
<ion-item>
<ion-label>Year</ion-label>
<ion-input type="tel" fixed [(ngModel)]="person.year"></ion-input>
</ion-item>
<ion-item-divider></ion-item-divider>
<ion-item>
<ion-label>Height (cm)</ion-label>
<ion-input type="tel" fixed [(ngModel)]="person.height"></ion-input>
</ion-item>
<ion-item>
<ion-label>Weight: (kg)</ion-label>
<ion-input type="tel" fixed [(ngModel)]="person.weight"></ion-input>
</ion-item>
<button ion-button block (click)="goToNextSlide()">Next</button>
</ion-slide>
<!-- Some more slides... -->
</ion-slides>
</ion-content>
If you need futher information, please let me know! I am appreciating any hints! :)

ionic 2 ion-inputs inside list

I am trying to make a custom item containing 3 inputs in a list in ionic 2, but it's not working. When I put only 1 input, there's no problem. But if I put other tag with "
<ion-item *ngFor="let pergunta of avaliacao; let i=index">
<ion-row>
<ion-input placeholder="Pergunta" [(ngModel)]="pergunta.descricao" clearInput></ion-input>
</ion-row>
<ion-row>
<ion-textarea [(ngModel)]="pergunta.opcoes"></ion-textarea>
</ion-row>
<ion-row>
<ion-label>Obrigatória</ion-label><ion-checkbox [(ngModel)]="pergunta.obrigatoria"></ion-checkbox>
</ion-row>
</ion-item>
I did it. It seems I can't put several input inside ion-item. Just replaced ion-item for div