Angular2 - Show/Hide section on selection of radio button

后端 未结 2 1777
醉酒成梦
醉酒成梦 2021-02-19 22:56

I have to show or hide sections based on selection of radio button

 

        
相关标签:
2条回答
  • 2021-02-19 23:28

    In Angular it can be achieved with *ngIf:

     <input name="options"  [(ngModel)]="options" type="radio" [value]="true" [checked]="options"/> Yes
    
     <input name="options"[(ngModel)]="options" type="radio" [value]="false" [checked]="!options"/> No
    
     <h2 *ngIf="options">Supply</h2>
     <h2 *ngIf="!options">Demand</h2>
    

    And no need to check if option==true or false, [checked]="options" and [checked]="!options" do the same.

    0 讨论(0)
  • 2021-02-19 23:31
    <div>
                <div class="radio">
                    <label><input type="radio" [(ngModel)]="model.prop" name="prop" value="A">A</label>
                </div>
                <div class="radio">
                    <label><input type="radio" [(ngModel)]="model.prop" name="prop" value="B">B</label>
                </div>
                <div class="radio">
                    <label><input type="radio" [(ngModel)]="model.prop" name="prop" value="C">C</label>
                </div>
        </div>
    
    <div *ngIf="model.prop === 'A'">A</div>
    <div *ngIf="model.prop === 'B'">B</div>
    <div *ngIf="model.prop === 'C'">C</div>
    

    if you want to preselect the value type then type

     model.prop = 'A';
    

    in your .ts file

    0 讨论(0)
提交回复
热议问题