Angular組件庫ng-zorro-antd實(shí)現(xiàn)radio單選框選擇
antd組件庫升級(jí)之后代碼不生效
項(xiàng)目業(yè)務(wù)之前的代碼設(shè)計(jì)了類似radio單選框取消選擇的相關(guān)邏輯,用的是下面類似的代碼實(shí)現(xiàn)的。但近期對(duì)ng-zorro-antd組件庫升級(jí)之后,下面的代碼不生效了。
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-radio-radiogroup',
template: `
<nz-radio-group [(ngModel)]="radioValue">
<label nz-radio (click)="click('A')" nzValue="A">A</label>
<label nz-radio nzValue="B">B</label>
<label nz-radio nzValue="C">C</label>
<label nz-radio nzValue="D">D</label>
</nz-radio-group>
`
})
export class NzDemoRadioRadiogroupComponent {
radioValue = 'A';
click(value: any) {
if (this.radioValue === value) {
this.radioValue = ''
}
}
}于是我到組件庫源碼里去找原因,并寫demo打斷點(diǎn)調(diào)試
- 將nz-radio-group綁定的radioValue值清空會(huì)首先走group組件下面的邏輯
writeValue(value: NzSafeAny): void {
this.value = value;
this.nzRadioService.select(value);
this.cdr.markForCheck();
}- nz-radio 和 nz-radio-group都是共用這個(gè)NzRadioService,且組件內(nèi)init時(shí)都有對(duì)selected$這個(gè)流做監(jiān)聽
@Injectable()
export class NzRadioService {
selected$ = new ReplaySubject<NzSafeAny>(1);
touched$ = new Subject<void>();
disabled$ = new ReplaySubject<boolean>(1);
name$ = new ReplaySubject<string>(1);
touch(): void {
this.touched$.next();
}
select(value: NzSafeAny): void {
this.selected$.next(value);
}
setDisabled(value: boolean): void {
this.disabled$.next(value);
}
setName(value: string): void {
this.name$.next(value);
}
}
// radio.component.ts ====> ngOnInit
this.nzRadioService.selected$.pipe(takeUntil(this.destroy$)).subscribe(value => {
const isChecked = this.isChecked;
this.isChecked = this.nzValue === value;
// We don't have to run `onChange()` on each `nz-radio` button whenever the `selected$` emits.
// If we have 8 `nz-radio` buttons within the `nz-radio-group` and they're all connected with
// `ngModel` or `formControl` then `onChange()` will be called 8 times for each `nz-radio` button.
// We prevent this by checking if `isChecked` has been changed or not.
if (
this.isNgModel &&
isChecked !== this.isChecked &&
// We're only intereted if `isChecked` has been changed to `false` value to emit `false` to the ascendant form,
// since we already emit `true` within the `setupClickListener`.
this.isChecked === false
) {
this.onChange(false);
}
this.cdr.markForCheck();
});- 當(dāng)監(jiān)聽完值改變后,后面又執(zhí)行了radio的click事件,把點(diǎn)擊哪個(gè)radio的value值傳過去了,所以之前的清空值操作就被覆蓋了。
private setupClickListener(): void {
this.ngZone.runOutsideAngular(() => {
fromEvent<MouseEvent>(this.elementRef.nativeElement, 'click')
.pipe(takeUntil(this.destroy$))
.subscribe(event => {
/** prevent label click triggered twice. **/
event.stopPropagation();
event.preventDefault();
if (this.nzDisabled || this.isChecked) {
return;
}
this.ngZone.run(() => {
// !!! again
this.nzRadioService?.select(this.nzValue);
if (this.isNgModel) {
this.isChecked = true;
this.onChange(true);
}
this.cdr.markForCheck();
});
});
});
}解決方法
清空值的操作加setTimeout 使組件庫內(nèi)部先執(zhí)行完click后續(xù)再執(zhí)行。
import { Component } from '@angular/core';
@Component({
selector: 'nz-demo-radio-radiogroup',
template: `
<nz-radio-group [(ngModel)]="radioValue">
<label nz-radio (click)="click('A')" nzValue="A">A</label>
<label nz-radio nzValue="B">B</label>
<label nz-radio nzValue="C">C</label>
<label nz-radio nzValue="D">D</label>
</nz-radio-group>
`
})
export class NzDemoRadioRadiogroupComponent {
radioValue = 'A';
click(value: any) {
if (this.radioValue === value) {
setTimeout(()=>{
this.radioValue = ''
})
}
}
}總結(jié)
其實(shí)組件庫單選radio本身是不支持取消選擇的,正解應(yīng)該是用checkbox實(shí)現(xiàn)相關(guān)的業(yè)務(wù)邏輯才對(duì),但很久之前的業(yè)務(wù)邏輯涉及到很多地方的修改,此時(shí)再換checkbox并且換樣式的話,改動(dòng)的還是比較大的,就先簡(jiǎn)單解決這個(gè)問題。
相關(guān)文章
Angular跨字段驗(yàn)證器中如何直接調(diào)用其它獨(dú)立的驗(yàn)證器
我們?cè)陂_發(fā)的時(shí)候都會(huì)用到表單,那么驗(yàn)證器就是必不可少的東西,這篇文章主要給大家介紹了關(guān)于在Angular跨字段驗(yàn)證器中如何直接調(diào)用其它獨(dú)立的驗(yàn)證器的相關(guān)資料,需要的朋友可以參考下2022-03-03
Angularjs注入攔截器實(shí)現(xiàn)Loading效果
angularjs作為一個(gè)全ajax的框架,對(duì)于請(qǐng)求,如果頁面上不做任何操作的話,在結(jié)果反回來之前,頁面是沒有任何響應(yīng)的,不像普通的HTTP請(qǐng)求,會(huì)有進(jìn)度條之類2015-12-12
簡(jiǎn)單談?wù)凙ngular中的獨(dú)立組件的使用
這篇文章主要介紹了簡(jiǎn)單談?wù)凙ngular中的獨(dú)立組件的使用的相關(guān)資料,通過實(shí)際案例向大家展示操作過程,操作方法簡(jiǎn)單快捷,實(shí)用性強(qiáng),需要的朋友可以參考下2022-08-08
AngularJS中使用three.js的實(shí)例詳解
這篇文章主要介紹了AngularJS中使用three.js的實(shí)例詳解,我將之前自己做的demo放到了angularJS的一個(gè)component中,其實(shí)一開始是沒有準(zhǔn)備用框架的但是后面發(fā)現(xiàn)需要進(jìn)行的雙向綁定越來越多,后期表單數(shù)據(jù)的變化量也很大,最后還是選擇用NG來做這些事情2017-07-07
AngularJS實(shí)現(xiàn)controller控制器間共享數(shù)據(jù)的方法示例
這篇文章主要介紹了AngularJS實(shí)現(xiàn)controller控制器間共享數(shù)據(jù)的方法,結(jié)合簡(jiǎn)單實(shí)例形式分析了AngularJS控制器數(shù)據(jù)共享的實(shí)現(xiàn)方法,需要的朋友可以參考下2017-10-10
Angularjs實(shí)現(xiàn)分頁和分頁算法的示例代碼
分頁是很多web應(yīng)用都會(huì)用到的,本篇文章主要介紹了Angularjs實(shí)現(xiàn)分頁和分頁算法的示例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下。2016-12-12

