如何正确禁用ngbRadioGroup?

时间:2019-10-10 10:45:48

标签: angular ng-bootstrap

我目前正在使用ng-bootstrap radio buttons,主要用于反应式表单,但也在实现ControlValueAccessor的自定义控件中使用。

我正在尝试找出禁用整个ngbRadioGroup的正确方法。

我可以通过调用.disable()来使用反应式表单禁用它。效果很好,但是:

问题1:使用模板驱动的方法禁用ngbRadioGroup的正确方法是什么?

问题2:是否可以使用Renderer2禁用它,或者这里有错误?例如我目前正在执行以下操作,如果ngbRadioGroup可以使用这种方法,那就太好了:

@ViewChildren('control') private controls: QueryList<ElementRef>;

setDisabledState?(isDisabled: boolean): void {
   this.controls.forEach(control => {
       this.renderer.setProperty(control.nativeElement, 'disabled', isDisabled);
   });
}

这是一个StackBlitz,它显示了到目前为止我调查的内容: https://stackblitz.com/edit/angular-vp1ozl

1 个答案:

答案 0 :(得分:1)

您可以使用ViewChild访问NgbRadioGroup实例

@ViewChild(NgbRadioGroup,{static:true}) ngbRadioRef:NgbRadioGroup;

然后您可以动态地将disable属性设置为true或false

disable(){
    this.ngbRadioRef.disabled = true;
  }

Example