简体   繁体   中英

<mat-radio-button> : No value accessor

I have this HTML:

 <mat-radio-group>
  <mat-radio-button  [(ngModel)]="searchType">
      And (Narrower search)
  </mat-radio-button>
  <mat-radio-button  [(ngModel)]="searchType">
       Or (Wider search)
  </mat-radio-button>
 </mat-radio-group>

I am getting this error:

Error: No value accessor for form control with unspecified name attribute

I tried using this instead:

 <mat-radio-button name="search-and"[(ngModel)]="searchType">
      And (Narrower search)
 </mat-radio-button>

and I get this error:

Error: No value accessor for form control with name: 'search-and'

so then I tried this:

 <mat-radio-button value="search-and" name="search-and" [(ngModel)]="searchType">
              And (Narrower search)
 </mat-radio-button>

and this:

  <mat-radio-button [value]="search-and" name="search-and" [(ngModel)]="searchType">
              And (Narrower search)
  </mat-radio-button>

and I still get this error:

Error: No value accessor for form control with name: 'search-and'

Ugghh my bad, ngModel belongs at the parent level:

  <mat-radio-group fxLayout="column" [(ngModel)]="searchType" (change)="onSearchTypeChange()">
     <mat-radio-button value="search-and" name="search-and">
          And (Narrower search)
     </mat-radio-button>
     <mat-radio-button  value="search-or" name="search-or">
         Or (Wider search)
    </mat-radio-button>
  </mat-radio-group>

Now everything works, no errors, not sure whether to use [value] or value though.

尝试在<mat-radio-group>而不是<mat-radio-button>上设置formControlName="SearchType"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM