<mat-radio-button>:没有值访问者

时间:2018-01-30 06:35:37

标签: javascript html angular angular-material2

我有这个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>

我收到此错误:

  

错误:没有带有未指定名称的表单控件的值访问器   属性

我尝试使用此代码:

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

我收到此错误:

  

错误:没有名称的表单控件的值访问器:   '搜索和'

所以我试过这个:

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

和此:

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

我仍然收到此错误:

  

错误:没有名称为'search-and'

的表单控件的值访问器

2 个答案:

答案 0 :(得分:0)

我的坏人,public class OffersRecyclerViewAdapter extends RecyclerView.Adapter<OffersRecyclerViewAdapter.ViewHolder> { private List<OffersModel> offersList; private Context context; private int lastSelectedPosition = -1; public OffersRecyclerViewAdapter(List<OffersModel> offersListIn, Context ctx) { offersList = offersListIn; context = ctx; } @Override public OffersRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.offer_item, parent, false); OffersRecyclerViewAdapter.ViewHolder viewHolder = new OffersRecyclerViewAdapter.ViewHolder(view); return viewHolder; } @Override public void onBindViewHolder(OffersRecyclerViewAdapter.ViewHolder holder, int position) { //since only one radio button is allowed to be selected, // this condition un-checks previous selections holder.selectionState.setChecked(lastSelectedPosition == position); } @Override public int getItemCount() { return offersList.size(); } public class ViewHolder extends RecyclerView.ViewHolder { public RadioButton selectionState; public ViewHolder(View view) { super(view); selectionState = (RadioButton) view.findViewById(R.id.offer_select); selectionState.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { lastSelectedPosition = getAdapterPosition(); notifyDataSetChanged(); } }); } } } 属于父级:

ngModel

现在一切正常,没有错误,不确定是否使用 <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> [value]

答案 1 :(得分:0)

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

相关问题