如何正确禁用 ngbRadioGroup?
How to correctly disable ngbRadioGroup?
我目前正在使用 ng-bootstrap radio buttons,主要使用 Reactive Forms,但也在实现 ControlValueAccessor
.
的自定义控件中
我正在尝试找出禁用整个 ngbRadioGroup
的正确方法。
我可以通过调用 .disable()
设法使用 Reactive Forms 禁用它。不过效果很好:
问题 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
您可以使用ViewChild访问NgbRadioGroup实例
@ViewChild(NgbRadioGroup,{static:true}) ngbRadioRef:NgbRadioGroup;
然后你可以动态设置禁用属性真或假
disable(){
this.ngbRadioRef.disabled = true;
}
我目前正在使用 ng-bootstrap radio buttons,主要使用 Reactive Forms,但也在实现 ControlValueAccessor
.
我正在尝试找出禁用整个 ngbRadioGroup
的正确方法。
我可以通过调用 .disable()
设法使用 Reactive Forms 禁用它。不过效果很好:
问题 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
您可以使用ViewChild访问NgbRadioGroup实例
@ViewChild(NgbRadioGroup,{static:true}) ngbRadioRef:NgbRadioGroup;
然后你可以动态设置禁用属性真或假
disable(){
this.ngbRadioRef.disabled = true;
}