2017-02-24 6 views
1

私は私はたくさんのことをsearcheが、私は、適切な取得していないコードイオン2読み取り]チェックボックスの値

<div class="form-group"> 
      <label ></label> 
      <div *ngFor="let mydata of activity" > 
       <label> 
         <input type="checkbox" 
          name="activity" 
          value="{{mydata.activity_id}}" 

          [checked]="activity.includes(mydata)" 

      (change)="changeSchedules(mydata)" 
          /> 
        {{mydata.activity_name}} 

       </label> 
      </div> 
     </div> 

スクリプトファイル

this.subscriptionForm = new FormGroup({ 

     activity: new FormArray(this.subscription.schedules.map(schedule => new FormControl(schedule)), Validators.minLength(1)) 
    }); 

changeSchedules(mydata: any) { 

    var currentScheduleControls: FormArray = this.subscriptionForm.get('activity') as FormArray; 
    var index = currentScheduleControls.value.indexOf(mydata); 


    if(index > -1) 
    {currentScheduleControls.removeAt(index) } 

    else 
{ 

    currentScheduleControls.push(new FormControl(mydata)); 
} 

} 

次しようとしたイオン2 のチェックボックスの値を読み取ることができません解決策 誰かがこれを理解するのに役立つことができます

答えて

2

あなたに役立つと思います

あなた.TSファイルコード

export class FormComponent { 
cbArr: string[]; 
    cbChecked: string[]; 
    constructor() { 
    this.cbArr = ['OptionA', 'OptionB', 'OptionC']; 
    this.cbChecked = []; 
    } 

    powers = ['Really Smart', 'Super Flexible', 
      'Super Hot', 'Weather Changer']; 

    model = new Hero(18, 'Dr IQ', this.powers[0], 'Chuck Overstreet'); 

    submitted = false; 



    // TODO: Remove this when we're done 
    get diagnostic() { return JSON.stringify(this.model); } 

updateCheckedOptions(chBox, event) { 
     var cbIdx = this.cbChecked.indexOf(chBox); 

     if(event.target.checked) { 
      if(cbIdx < 0){ 
       this.cbChecked.push(chBox); 
      console.log(chBox); 
      } 

     } else { 
      if(cbIdx >= 0){ 
      this.cbChecked.splice(cbIdx,1); 
       console.log(cbIdx); 
      } 

     } 
    } 
    updateOptions() { 
    console.log(this.cbChecked); 
    } 
    ///////////////////////////// 

} 

とあなたのhtmlコード

 <label for="options">Options :</label> 
     <div *ngFor="let cbItem of cbArr"> 
      <label> 
      <input type="checkbox" 
        name="options" 
        value="{{cbItem}}" 
        [checked]="cbChecked.indexOf(cbItem) >= 0" 
        (change)="updateCheckedOptions(cbItem, $event)"/> 
      {{cbItem}} 
      </label> 
<button (click)="updateOptions()">Post</button> 

updateOptions()選択したすべてのチェックボックスの値

+0

ありがとうございました – ganesh

1

を例

のこのコードを試してみてください取得します

@Component({ 
 
    templateUrl: 'example-page.html' 
 
}) 
 
class ExamplePage { 
 
    cb_value: boolean; 
 

 
    updateCbValue() { 
 
    console.log('Something new state:' + this.cb_value); 
 
    } 
 
}
<ion-list> 
 

 
    <ion-item> 
 
    <ion-label>Something</ion-label> 
 
    <ion-checkbox [(ngModel)]="cb_value" (ionChange)="updateCbValue()"></ion-checkbox> 
 
    </ion-item> 
 

 
</ion-list>

関連する問題