代码之家  ›  专栏  ›  技术社区  ›  Corcor

如果选中单选按钮,则以角度5显示元素

  •  0
  • Corcor  · 技术社区  · 6 年前

    我正在尝试获取一个单选按钮,以便在选中该单选按钮时显示元素。我试过多种不同的方法[(ngModel)会抛出错误。

    这是我的表格.ts:

    import {Component, Input} from '@angular/core';
    import {FormGroup} from "@angular/forms";
    import { MdRadioChange } from '@angular/material';
    @Component({
      selector: 'activation-role',
      template: `
        <div class="form-group" [formGroup]="group" >
          <mat-radio-group formControlName="role" [attr.data-optional]="canViewDiv">
            <mat-radio-button #{{role.id}} (change)="onRadioChange($event)" class="radio-button-vertical" *ngFor="let role of activation_roles" [value]="role.id">
              <div>{{role.label}}</div> 
              <mat-hint>{{role.additionalText}}</mat-hint>
            </mat-radio-button>
          </mat-radio-group>
        </div>`
    
    })
    
    export class ActivationRoleComponent {
    
      @Input() group: FormGroup;
    
      canViewDiv: boolean = true;
    
      onRadioChange(event: MdRadioChange) {
    
        if (event.value === 'option1') {
          this.canViewDiv = true;
    
          console.log(event.value);
          console.log(this.canViewDiv);
        } else {
          this.canViewDiv = false;
    
          console.log(event.value);
          console.log(this.canViewDiv);
        }
      }
    
    
      private activation_roles = [{
        id: 'option1',
        value: 'Option one',
        label: 'Option one',
        status: true,
        additionalText: 'This is additional text'
      },{
        id: 'option2',
        value: 'Option two',
        label: 'Option two',
        status: false,
        additionalText: 'This is additional text two'
      },{
        id: 'option3',
        value: 'Option three',
        label: 'Option three',
        status: false,
        additionalText: 'This is additional text three'
      }];
    
    }export class ActivationRoleComponent {
    
      @Input() group: FormGroup;
    
      canViewDiv: boolean = true;
    
      onRadioChange(event: MdRadioChange) {
    
        if (event.value === 'option1') {
          this.canViewDiv = true;
    
          console.log(event.value);
          console.log(this.canViewDiv);
        } else {
          this.canViewDiv = false;
    
          console.log(event.value);
          console.log(this.canViewDiv);
        }
      }
    
    
      private activation_roles = [{
        id: 'option1',
        value: 'Option one',
        label: 'Option one',
        status: true,
        additionalText: 'This is additional text'
      },{
        id: 'option2',
        value: 'Option two',
        label: 'Option two',
        status: false,
        additionalText: 'This is additional text two'
      },{
        id: 'option3',
        value: 'Option three',
        label: 'Option three',
        status: false,
        additionalText: 'This is additional text three'
      }];
    
    }
    

    这是我的html表单:

    <mat-accordion>
            <!-- ROLE ACCORDION -->
            <mat-expansion-panel class="activation-form__role" expanded="true" #roleAccordion>
              <mat-expansion-panel-header #roleAccordionHeader>Your Role</mat-expansion-panel-header>
    
              <activation-role [group]="roleGroup"></activation-role>
    
              <mat-action-row>
                <button mat-button color="primary" class="activation-form__role__next-btn" (click)="expandStep(1)" [disabled]="roleGroup.invalid" >Next</button>
              </mat-action-row>
            </mat-expansion-panel>
    
            <mat-expansion-panel *ngIf="roleGroup?.data-optional === 'true'" class="rmb-form__corporate-details" #strataManagerDetailsAccordion [disabled]="detailsGroup.invalid">
            </mat-expansion-panel>
    <mat-accordion>
    

    感谢您的帮助。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Nabil Shahid    5 年前

    你的方法有些问题。首先,您尝试使用一个布尔值,它位于子组件(即父组件(即窗体组件)中的激活角色)中。更改后需要将值从子组件发射到父组件,然后基于该值显示和隐藏父组件中的div。

    另外,在激活角色中,您不需要通过传递作为输入来使用父组件的表单组,因为该组件中的表单只有一个单选按钮。您可以实现您想要的,而无需在activationroute组件中使用formgroup和ngModel。

    我为您创建了一个stackblitz解决方案,其中我实现了与您的组件类似的激活角色组件。我不知道表单组件,所以我创建了自己的自定义表单并在激活角色中使用它,并且基于激活角色组件中的单选按钮,隐藏或显示了一个mat expnasion面板。 以下是解决方案的链接: Show hide div based on radio 在这个解决方案中,我从子组件发出单选按钮的值,并在父组件中使用它。试着理解解决方案中正在做的事情,这样你就可以更好地理解角度的输入和输出。 Input Output in Angular

    @Gaurang Sondagar更新 stackblitz 根据您的要求。它包含两个组件, form-component expansion-panel . 扩展面板包含面板和无线电组。

    allRadioPanels 阵列,用于渲染面板。此数组的每个对象都包含绑定的 SelectedValue Comments CommentsRequiredValue

    评论必选值 如果等于 选定值 是空白的。

    例如,对于面板2 评论必选值 Option 2 选定值 方案2 也。如果您从选项2更改Panel 2 radio的值或在其中输入注释,则最初禁用的submit按钮将启用。

    所有面板的工作方式都类似。