无法使ngx-bootstrap无线电工作,没有值访问器

问题描述 投票:2回答:1

这一直在推动我。我几乎使用ngx-bootstrap手册中的示例:https://valor-software.com/ngx-bootstrap/#/buttons#radio-reactiveforms并且它不起作用。这是我的模板和组件:

模板:

<pre class="card card-block card-header">{{ myForm.value | json }}</pre>
<form [formGroup]="myForm" class="form-inline">
  <div class="form-group">
    <div class="btn-group" btnRadioGroup formControlName="radio">
      <label btnRadio="A" class="btn btn-primary">A</label>
      <label btnRadio="B" class="btn btn-primary">B</label>
      <label btnRadio="C" class="btn btn-primary">C</label>
    </div>
  </div>
</form>

零件:

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';

@Component({
  selector: 'demo-buttons-radio-reactiveforms',
  templateUrl: './keyed-payment.component.html',
  styleUrls: ['./keyed-payment.component.scss']
})
export class KeyedPaymentComponent implements OnInit {
  myForm: FormGroup;

  constructor(private formBuilder: FormBuilder) {}

  ngOnInit() {
    this.myForm = this.formBuilder.group({
      radio: 'C'
    });
  }
}

另外我还在app.module.ts中添加了以下内容:

import { FormsModule } from '@angular/forms';
import { ReactiveFormsModule } from '@angular/forms';

并在导入数组中:

imports: [
   ....
   FormsModule,
   ReactiveFormsModule,
   ....
],

这是我得到的错误:

错误错误:FormControlName._setUpControl(表单中的FormGroupDirective.addControl(forms.js:6658)处的setUpControl(forms.js:2302)处的_throwError(forms.js:2432)处的名称为'radio'的表单控件没有值访问器(表单) .js:7308)在checkAndUpdateNireInline(core.js:12365)的checkAndUpdateNireInline(core.js:12365)的FormControlName.ngOnChanges(forms.js:7221),checkAndUpdateNode(core.js:13836),checkChendAndUpdateNode(core.js:13836),debugCheckAndUpdateNode(core.js: 14729)在debugCheckDirectivesFn(core.js:14670)

有任何想法吗?

angular angular-reactive-forms ngx-bootstrap
1个回答
2
投票

我已经明白了。我所要做的就是将它添加到我顶部的app.module.ts:

import { ButtonsModule } from 'ngx-bootstrap';

这在我的进口中:

imports: [
    ....
    FormsModule,
    ReactiveFormsModule,
    ButtonsModule,
    ....
]
© www.soinside.com 2019 - 2024. All rights reserved.