2017-09-12 144 views
2

您好我有一个问题 - 离子2离子3 - 离子的复选框内ngfor并用条件,如ngIf或ngSwitch不工作离子2离子3 - 内部ngIf或ngSwitch离子复选框不工作

我贴我的代码在网上有工作和不工作的版本..

https://forum.ionicframework.com/t/ionic-2-ionic-3-ion-checkbox-inside-ngif-or-ngswitch-not-working/105099

工作

<ion-list> 
       <ion-item *ngFor=" let answer of question.Answers"> 
          <ion-label>{{answer.Description}}</ion-label> 
          <ion-checkbox (change)="change($event, answer)"></ion-checkbox> 
       </ion-item> 
      </ion-list> 

enter image description here 不工作1

<ion-list> 
       <ion-item *ngFor="let answer of question.Answers"> 
        <ng-container *ngIf="QuestionType.MutipleChoice==question.QuestionType_Id"> 
          <ion-label>{{answer.Description}}</ion-label> 
          <ion-checkbox (change)="change($event, answer)"></ion-checkbox> 
        </ng-container>      
       </ion-item> 
      </ion-list> 

不灵2

<ion-list> 
       <ion-item *ngFor=" let answer of question.Answers"> 
        <ng-container [ngSwitch]="question.QuestionType_Id"> 
         <ng-container *ngSwitchCase="QuestionType.MutipleChoice"> 
          <ion-label>{{answer.Description}}</ion-label> 
          <ion-checkbox (change)="change($event, answer)"></ion-checkbox> 
         </ng-container> 
        </ng-container> 
       </ion-item> 
      </ion-list> 

这一个是工作,但只有等到我想要进入里面的复选框... 使用模板试过,跨度标签而不是NG-容器...等...

<ion-list> 
       <ion-item> 
        <div *ngFor="let answer of question.Answers"> 
         <div [ngSwitch]="question.QuestionType_Id"> 
          <span *ngSwitchCase="QuestionType.YesNo"> 
               <span *ngIf="answer.Description=='Yes'" style=" display: inline-block;"> 
                  <button ion-fab right><ion-icon name="checkmark"></ion-icon></button> 
                 </span> 
               <span *ngIf="answer.Description=='No'" style=" display: inline-block;"> 
                   <button ion-fab color="danger" left><ion-icon name="close"></ion-icon></button> 
                 </span> 
          </span> 
          <ng-container *ngSwitchCase="QuestionType.OneChoise"> 
           {{ answer.Description }} 
          </ng-container> 
          <ng-container *ngSwitchCase="QuestionType.MutipleChoice"> 
           {{ answer.Description }} 
           <!-- <ion-label>{{ answer.Description }}</ion-label> --> 
           <!-- <ion-checkbox color="dark" checked="answer.checked" [(ngModel)]="answer.checked"></ion-checkbox> 
              --> 
          </ng-container> 

         </div> 
        </div> 
       </ion-item> 
      </ion-list> 

enter image description here 的ngSwitch适用于其他类型LIK电子 - 是的,或者一个选择,但添加离子复选框时不在这里。 json也在工作,这意味着没有复选框或没有开关,我可以看到多个选项。

任何想法,我可以解决这个问题?我错过了什么? Puling现在听到一天...

有没有人解决它?

回答

1

我认为这将解决问题。

export class HomePage { 
 
    
 
    questions = [{id:1,text:'Question 1', answers:[{id:1},{id:2}]},{id:2,text:'Question 2', answers:[{id:11},{id:22}]}] 
 
    
 
    constructor(private navController: NavController, private service: Service, private formBuilder:FormBuilder) { 
 
    this.surveyForm = this.formBuilder.group({ 
 
     questions: formBuilder.array([]) 
 
    }) 
 

 
    for (var i = 0; i < this.questions.length; i++) { 
 
     // get multiple 
 
     let question = formBuilder.group({ 
 
      question_id: [this.questions[i].id, Validators.required], 
 
      answer_ids: formBuilder.array([]) 
 
     }); 
 
     this.surveyForm.controls['questions'].push(question); 
 
    } 
 
    } 
 

 
    onChange(id, isChecked, index) { 
 
    const answers = <FormArray>this.surveyForm.controls.questions.controls[index].controls.answer_ids 
 
    
 
    if(isChecked) { 
 
     answers.push(new FormControl(id)) 
 
    } else { 
 
     let idx = answers.controls.findIndex(x => x.value == id) 
 
     answers.removeAt(idx) 
 
    } 
 
    } 
 
}
<ion-header> 
 
    <ion-navbar> 
 
    <ion-title>{{ appName }}</ion-title> 
 
    </ion-navbar> 
 
</ion-header> 
 

 
<ion-content padding> 
 
<div *ngIf="surveyForm"> 
 
    <form [formGroup]="surveyForm"> 
 
     <div formArrayName="questions"> 
 
     <div *ngFor="let question of questions; let i = index" [formGroupName]="i" padding-bottom> 
 
      <ion-row> 
 
      <ion-col col-10> 
 
       <h5>{{ question.text }}</h5> 
 
       <ng-container> 
 
       <ion-list formArrayName="answer_ids"> 
 
        <div *ngFor="let choice of question.answers; let j = index"> 
 
        <ion-item> 
 
         <ion-label style="white-space: normal;">{{ choice.id }}</ion-label> 
 
         <ion-checkbox (ionChange)="onChange(choice.id, $event.checked, i)" value="choice.id"></ion-checkbox> 
 
        </ion-item> 
 
        </div> 
 
       </ion-list> 
 
       </ng-container> 
 
      </ion-col> 
 
      </ion-row> 
 
     </div> 
 
     </div> 
 
    </form> 
 
    </div> 
 
    
 
    <pre>{{surveyForm.value | json}}</pre> 
 
</ion-content>

http://plnkr.co/edit/yV94ZjypwBgHAlb0RLK2?p=preview 现在尝试它。

+0

我玩过它,它确实解决了它。 –