I want to perform some action whenever the input value or the slider is changed, but why is ngOnchanges not getting triggered when text or range input is changed?
Controller:
export class APage implements OnChanges {
#Input() total: string;
#Input() percentage: string;
constructor() {
}
ngOnChanges(changes: {[propName: string]: SimpleChange}) {
console.log('ngOnChanges');
}
}
Template:
<ion-list>
<ion-item>
<ion-label floating>Total</ion-label>
<ion-input type="text" [(ngmodel)]="total"></ion-input>
</ion-item>
<ion-item>
<ion-label stacked>Percentage: {{percentage}} %</ion-label>
<ion-range [(ngModel)]="percentage" min="0" max="100">
</ion-range>
</ion-item>
</ion-list>
Update1:
Add #Input decorater as per the suggestion which didn't help.
Update2:
This works:
ion-range [(ngModel)]="percentage" min="0" max="100" (ionChange)="inputChanged()">
But it doesn't work for ion-input:
<ion-input type="text" [(ngmodel)]="total" (ionChange)="inputChanged()"></ion-input>
Update 3:
It worked with keyup:
<ion-input type="text" [(ngmodel)]="total" (keyup)="inputChanged()"></ion-input>
You might need to decorate aData with #Input()
Edit:
It's probably that you need to hook into the ionic events via the template. If it's beta 8, the event is called (ionChange), pre beta8 it's (change) , according to the changelog
so something like <ion-range [(ngModel)]="percentage" (ionChange)="changeFunc()" min="0" max="100">
Related
I am trying to take amount to be transacted as input from user deduct it from the wallet of the user and then display the updated wallet. The wallet already is pre-loaded with 100 units of money. But when I run the app only 100 is displayed and the changes are not reflected. what do I do ?
Here is my html code
<ion-row>
<ion-col class="txt2" size="6" text-left >Company<br><ion-text class="bal">Balance & gms</ion-text>
</ion-col>
<ion-col class="text2" size="6" text-right ><ion-item><ion-input type="number" value="{{ wallet }}" [(ngModel)]="wallet" class="ion-text-end"></ion-input>
</ion-item>
</ion-col>
</ion-row>
<ion-item class="transact-display">
<ion-input type="number" placeholder="Enter amount" [(ngModel)]="amount" name="amount"></ion-input>
</ion-item>
<ion-button class="transact" type="submit" expand="block" (click)="transact()">Transact</ion-button>
The ts file
import { Component, OnInit } from '#angular/core';
import { Router } from '#angular/router';
#Component({
selector: 'app-transaction',
templateUrl: './transaction.page.html',
styleUrls: ['./transaction.page.scss'],
})
export class TransactionPage implements OnInit {
constructor(public router : Router) {}
wallet: number = 100;
amount: number ;
ngOnInit() {
}
transact() {
this.router.navigateByUrl('/congratulation');
this.wallet = this.wallet - this.amount;
return this.wallet;
}
}
Actual behaviour:
No change in the displayed amount. 100 is only displayed even after the Transact button is clicked.
Expected behaviour:
when the user logs in for the 1st time, 100 should be displayed. When the user transacts amount, say 20, 20 should be deducted from 100 and 80 should be displayed.
Angular is 2 way data binding framwwork:
Use Angular interpolation:
.html
<ion-item>
{{wallet}}
</ion-item>
<ion-item class="transact-display">
<ion-input type="number" placeholder="Enter amount" [(ngModel)]="amount" name="amount"></ion-input>
</ion-item>
<ion-button class="transact" type="submit" expand="block" (click)="transact()">Transact</ion-button>
.ts
wallet: number = 100;
amount: number ;
ngOnInit() {
}
transact() {
this.wallet = this.wallet - this.amount; // this will update wallet automatically.
this.router.navigateByUrl('/congratulation');
}
In my ionic select i got output like small down arrow rather than a full select tag and for input tag just a blank space. I can enter data but UI not looks good. Is there any css i need to add.
<ion-card class='sc-ion-card-md-h'>
<ion-card-header>
<ion-card-subtitle>Name:</ion-card-subtitle>
<ion-card-title>
<ion-select #inputName id="username" class="form-control">
<ion-select-option>SELECT</ion-select-option>
<ion-select-option>Monicka</ion-select-option>
<ion-select-option>Hema</ion-select-option>
<ion-select-option>Ramesh</ion-select-option>
<ion-select-option>Madhavan</ion-select-option>
<ion-select-option>Aadhavan</ion-select-option>
<ion-select-option>Madhan</ion-select-option>
<ion-select-option>Prasanth</ion-select-option>
</ion-select>
</ion-card-title>
</ion-card-header>
<ion-card-header>
<ion-card-subtitle>Date:</ion-card-subtitle>
<ion-card-title>
<ion-input type="text" #inputDate id="date" class="form-control"></ion-input>
</ion-card-title>
</ion-card-header>
</ion-card>
Output looks like this...
It seems like you are trying to convert a Bootstrap form over to Ionic and you are bringing some jQuery thinking along with it.
This is how you would do it with an Ionic page:
page.html
<ion-header>
<ion-toolbar>
<ion-title>card-select</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<ion-card>
<ion-list>
<ion-item>
<ion-label position="stacked">Name:</ion-label>
<ion-select [(ngModel)]="username" placeholder="SELECT">
<ion-select-option>Monicka</ion-select-option>
<ion-select-option>Hema</ion-select-option>
<ion-select-option>Ramesh</ion-select-option>
<ion-select-option>Madhavan</ion-select-option>
<ion-select-option>Aadhavan</ion-select-option>
<ion-select-option>Madhan</ion-select-option>
<ion-select-option>Prasanth</ion-select-option>
</ion-select>
</ion-item>
<ion-item>
<ion-label position="stacked">Date:</ion-label>
<ion-input [(ngModel)]="date"></ion-input>
</ion-item>
</ion-list>
</ion-card>
</ion-content>
page.ts
import { Component, OnInit } from '#angular/core';
#Component({
selector: 'app-card-select',
templateUrl: './card-select.page.html',
styleUrls: ['./card-select.page.scss'],
})
export class CardSelectPage implements OnInit {
username: string;
date: string;
constructor() { }
ngOnInit() {
}
}
notes
You don't need all those classes you put on.
Using label position="stacked" puts it above the input.
You dont need to bind the name like #username or id="username". What you do is put a variable on the page behind it and then use ngModel to bind to it. Any change that is made to the user interface (typing into a box, selecting an option) is then automatically set to the variable in the page. This works both ways because of the [()] syntax, so if you change username with something like this.username = "superman"in the code then the input box on the page will automatically update to match that value as well.
You don't need type="text" on the input, its the default.
You can use the placeholder attrib to pass some SELECT text instead of having an extra select option.
I'm trying to upgrade a project I have from ionic v3 to ionic v4. However, I have a problem with template driven forms. I have the following:
<ion-list [class]="myradio.isValidClass" radio-group [(ngModel)]="myradio.value" ngDefaultControl>
<ion-radio-group>
<ion-item>
<ion-label>Option1</ion-label>
<ion-radio value="option1" [disabled]="myradio.disable"></ion-radio>
</ion-item>
<ion-item>
<ion-label>Option2</ion-label>
<ion-radio value="option2" [disabled]="myradio.disable"></ion-radio>
</ion-item>
</ion-radio-group>
</ion-list>
<button (click)="submit()">Submit</button>
and in the corresponding ts file:
#Component({
selector: 'mycomponent',
templateUrl: './mycomponent.component.html',
styleUrls: ['./mycomponent.component.scss'],
})
export class MyComponent {
private myradio = {
value: '',
disable: false,
isValidClass: 'ng-valid'
}
constructor() {
}
submit() {
console.log(this.myradio);
}
}
What I was expecting after I have chosen a radio and submit, is to give me console.log(this.myradio) a value, e.g. this.myradio.value=option1 or option2. However, it gives the initial value, i.e. an empty string.
In ionic v3 it was working as expected...what am I missing?
put the ngModel in the ion-radio-group tag. ion-list tag does not support ngModel that's the reason the selected value not bound to the model
<ion-list [class]="myradio.isValidClass" radio-group>
<ion-radio-group [(ngModel)]="myradio.value">
<ion-item>
<ion-label>Option1</ion-label>
<ion-radio value="option1" [disabled]="myradio.disable"></ion-radio>
</ion-item>
<ion-item>
<ion-label>Option2</ion-label>
<ion-radio value="option2" [disabled]="myradio.disable"></ion-radio>
</ion-item>
</ion-radio-group>
</ion-list>
I have a form with array values in this array format since I don't know how to dynamically add form inputs, I had to manually do it. Now when I tried submitting the form, this error got display ERROR TypeError: Cannot read property 'value' of undefined
post.html
<ion-item class="mylist chat-item item-remove-animate item-avatar item-icon-right list card animated fadeInUp">
<ion-input type="text" readonly name="party" placeholder="APC" value="apc"></ion-input>
<ion-input type="number" placeholder="Total Votes" name="vote" #vote></ion-input>
</ion-item>
<ion-item class="mylist chat-item item-remove-animate item-avatar item-icon-right list card animated fadeInUp">
<ion-input type="text" readonly name="party" value="pdp"></ion-input>
<ion-input type="number" placeholder="Total Votes" name="vote" #vote></ion-input>
</ion-item>
<ion-item class="mylist chat-item item-remove-animate item-avatar item-icon-right list card animated fadeInUp">
<ion-input type="text" readonly name="party" value="apga"></ion-input>
<ion-input type="number" placeholder="Total Votes" name="vote" #vote></ion-input>
</ion-item>
post.ts
#ViewChild("party") party;
#ViewChild("vote") vote;
#ViewChild("phones")phones;
submit(){
let data = {
party: this.party.value,
vote: this.vote.value,
phone: this.phones.value
};
}
Please how can I pass these array values?
Thanks
Use ngModel.
post.html
<ion-input type="number" placeholder="Total Votes" name="vote" [(ngModel)]="party"></ion-input>
post.ts
export class Post {
party;
constructor() {}
submit(){
let data = {
party: this.party };
}
}
check this example
I'm an ionic beginner, I'm trying to push user input to Firebase,
in my html file:
<ion-item>
<ion-label>E-mail </ion-label>
<ion-input type="email" value="" placeholder="Type your e-mail"></ion-input>
</ion-item>
I have the inputtext field, but I handle firebase operations in my ts file, how can I send this input value to my ts file so I can push it?
In the component class file (what you call ts file), you can add a class member, let's call it email:
#Component({
...
})
export class MyComponent {
private email: string;
}
And you can bind it in your template with ngModel:
<ion-item>
<ion-label>E-mail </ion-label>
<ion-input [(ngModel)]="email" type="email" value="" placeholder="Type your e-mail"></ion-input>
</ion-item>