Hello Ionic community!
I have a problem passing a value from the selector to a typescript method. Here is the code:
<ion-select #C (ionChange)="changeSelect(C.value)">
<ion-option value="1">U1</ion-option>
</ion-select>
Passing the value works, but somehow the comparison doesnt. I changed "line == ‘U1’ " to “line == line” just to test and it worked. Why isn’t it passing the string correctly or why can’t I compare it?
changeSelect(line: string) {
if (line == 'U1') {
this.stations.push({ text: 'TEXT' });
}
}
Thank you in advance!
Regards,
Laurids
Why aren’t you just binding via ngModel
, so you will already have the value in the component controller code?
l4ur1ds:
Thank you in advance!
Regards,
Laurids
I wasn’t aware of that. How would that look in my implementation? Sorry I super bad with angular
l4ur1ds:
I have a problem passing a value from the selector to a typescript method. Here is the code:
<ion-select #C (ionChange)=“changeSelect(C.value)”>
<ion-option value=“1”>U1</ion-option>
</ion-select>
Passing the value works, but somehow the comparison doesnt. I changed "line == ‘U1’ " to “line == line” just to test and it worked. Why isn’t it passing the string correctly or why can’t I compare it?
changeSelect(line: string) {
if (line == ‘U1’) {
this.stations.push({ text: ‘TEXT’ });
}
}
thank you! but how will I pass it to my method then?
You don’t have to. Whatever property you bind [(ngModel)]
to will automatically contain the currently selected value.
l4ur1ds
January 30, 2017, 10:08pm
7
Thanks! So I just ask "property" == "myValue"
?
I would suggest taking the time to work through the Tour of Heroes , after which this should all make sense to you.
l4ur1ds
January 30, 2017, 10:26pm
9
Thank you very much. What I still don’t get is how I will make an action depending on what I selected. (sorry my brain is a though one and I am like 7 hours into this which makes it even harder).
I want
if(selectedValue = aValue1){
doSomething
}
else if(selectedValue = aValue2){
doSomething
}
etc.
Sorry for making you a hard time.
It looks like you are comparing the label and not the value of the selected option. The ion-option in your select has value set to 1 and not U1
<ion-option value="1">U1</ion-option>
Try this if it works
changeSelect(line: string) {
if (line == "1") {
this.stations.push({ text: 'TEXT' });
}
}
l4ur1ds
January 31, 2017, 12:09pm
11
Thanks for your reply, sadly this didn’t change anything
[(ngMode)]=“dataTocheck”
if(dataTocheck== aValue1){
doSomething
}
do something like this
l4ur1ds
January 31, 2017, 12:25pm
13
Thank you for your reply, how do I need to compare dataToCheck it tells me this in the console:
Cannot find name 'dataToCheck'.
L25: if (dataToCheck == 'U1') {
L26: this.showAlert();
1 Like
wp1
January 31, 2017, 12:29pm
14
In your component Add [(ngModel)]
<ion-select [(ngModel)]="gender" (ionChange)="changeSelect(gender)">
<ion-option value="f" selected="true">Female</ion-option>
<ion-option value="m">Male</ion-option>
</ion-select>
In your controller declare:
public gender:string;
constructor() {}
changeSelect(gender){
if(gender==‘f’)…
}
1 Like
l4ur1ds
January 31, 2017, 12:42pm
15
ohh it finally worked thank you so much!!
You’re really overcomplicating this. There is no need to pass anything to the change handler. It is already in the bound object property.
changeSelect() {
if (this.gender === 'M')
}
wp1
February 7, 2017, 11:02am
17
You’re in right @rapropos , is not need to pass any parameter.
How can I do same code using button?
[(ngMode)]=“height”
[(ngMode)]=“weight”
Get your plan is my button including next function in that
next(){
if(height== h1 || weight==w1 ){
doSomething
}
else{
do something like this
}
}
Can I get the proper solution for this?
What happens when you try to use that code?
It well work just compare passed value with 1 insted of “U1”