1

I have an array that i make use of it to dispaly

<ion-item (click)="toggleLanguages()">
          Languages
          <ion-icon name="add" item-right *ngIf="languageShow"></ion-icon>
          <ion-icon name="remove" item-right *ngIf="languageHide"></ion-icon>
        </ion-item>
        <div *ngIf="languageHide">

          <!-- All radio's in a radio group -->

          <ion-list radio-group [(ngModel)]="selectedLanguage" (ionChange)="doSomething(language)">
            <ion-item *ngFor="let language of languageArray">
              <ion-label >{{language.language_name}}</ion-label>
              <ion-radio item-left [value]="language"></ion-radio> 
            </ion-item>
          </ion-list>

        </div>

as you can see i am using ionchange to call a funtion

Problems:

when ever my languageHide variables become true my doSomething funtion is triggering

i am sending language object as parameter to my doSomething(language) function but in my console log i am getting undefined for language object.

here is my .ts file

doSomething(languages){

console.log("invoking dosomething");
console.log("checking languages "+ JSON.stringify(languages));//here it is not printing
}

toggleLanguages(){
  console.log("invoking toggleLanguages");
  this.languageShow = !this.languageShow;
  this.languageHide = !this.languageHide;
}

from the above two function i am first invoking toggleLanguages() function once i invoked this funtion my this.languageHide variable becomes true then my (ionChange) function is triggering automatically.

Community
  • 1
  • 1
Mohan Gopi
  • 7,606
  • 17
  • 66
  • 117

1 Answers1

0

I don't have any experience with Ionic, but you should be able to pass $event:

(ionChange)="doSomething($event)

You can then print $event in console to see how to extract the data you need:

doSomething(event: any){
    console.log(event);
}

You can't pass language variable because it's not visible in ion-list scope, it is defined below.

Stefan Svrkota
  • 48,787
  • 9
  • 98
  • 87
  • thanks but i tried that part if i use $event i am not able to get the language object i am getting the `language.language_name` printed in my console.log but i need the entire language object – Mohan Gopi Dec 12 '16 at 11:47
  • @MohanGopi That doesn't make sense, if you set `[value]="language"`, then you should get whole object. Are you sure you have't changed `value` to `[value]="language.language_name"` in the meantime? – Stefan Svrkota Dec 12 '16 at 11:49
  • i am very sure i have not changed it – Mohan Gopi Dec 12 '16 at 11:50
  • @MohanGopi Why don't you simply use `selectedLanguage` variable for getting current value since you used `NgModel` directive to bind selected value to it? You might have to change `[value]` to `[ngValue]` in order for that to work though. – Stefan Svrkota Dec 12 '16 at 11:51
  • if i use `[ngValue]` i am getting error as `ngValue isn't known property of ion-radio` i have done my code by see this website http://ionicframework.com/docs/v2/api/components/radio/RadioGroup/ – Mohan Gopi Dec 12 '16 at 11:55
  • Thanks i got it worked i found that the problem was because of ngmodel i am updating the value from inital stage once i commented that updation i got the value thanks for the information. – Mohan Gopi Dec 12 '16 at 12:01
  • I have one question because of languageHide become true my ionChange is automaticaly gets triggers how can i stop it – Mohan Gopi Dec 12 '16 at 12:14