亚洲免费在线视频-亚洲啊v-久久免费精品视频-国产精品va-看片地址-成人在线视频网

您的位置:首頁技術(shù)文章
文章詳情頁

Angular組件庫ng-zorro-antd實現(xiàn)radio單選框選擇

瀏覽:245日期:2022-06-10 08:53:48
目錄
  • antd組件庫升級之后代碼不生效
  • 解決方法
  • 總結(jié)

antd組件庫升級之后代碼不生效

項目業(yè)務(wù)之前的代碼設(shè)計了類似radio單選框取消選擇的相關(guān)邏輯,用的是下面類似的代碼實現(xiàn)的。但近期對ng-zorro-antd組件庫升級之后,下面的代碼不生效了。

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ào)試

  • 將nz-radio-group綁定的radioValue值清空會首先走group組件下面的邏輯
writeValue(value: NzSafeAny): void {
    this.value = value;
    this.nzRadioService.select(value);
    this.cdr.markForCheck();
  }
  • nz-radio 和 nz-radio-group都是共用這個NzRadioService,且組件內(nèi)init時都有對selected$這個流做監(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();
      });
  • 當監(jiān)聽完值改變后,后面又執(zhí)行了radio的click事件,把點擊哪個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é)

其實組件庫單選radio本身是不支持取消選擇的,正解應(yīng)該是用checkbox實現(xiàn)相關(guān)的業(yè)務(wù)邏輯才對,但很久之前的業(yè)務(wù)邏輯涉及到很多地方的修改,此時再換checkbox并且換樣式的話,改動的還是比較大的,就先簡單解決這個問題。

標簽: JavaScript
主站蜘蛛池模板: 国产女人在线观看 | 欧美亚洲国产片在线观看 | 99成人免费视频 | gayxxxxgay中国老头 | 久久精品99毛片免费 | 亚洲国产欧洲精品路线久久 | 国产制服 国产制服一区二区 | 美国一级片在线观看 | 亚洲网站www| 香蕉97碰碰视频免费 | 日产一区二区三区四区 | 亚洲精品不卡在线 | 国产在线视频精品视频免费看 | 国产在线a| 国产精品特级毛片一区二区三区 | 国产亚洲一区二区手机在线观看 | 日鲁夜鲁鲁狠狠综合视频 | 国产一级特黄全黄毛片 | 国产极品精频在线观看 | 久久网在线 | 91久久| 亚洲国语在线视频手机在线 | 久草在线网址 | 国内自拍第五一页 | 美女张开腿双腿让男人桶 | 2020毛片 | 日本三级中文字幕 | 3至13呦女毛片 | 免费人成在线观看网站 | 久久久精品免费视频 | 欧美一区二区视频 | 美女视频黄a全部免费专区一 | 午夜一级做a爰片久久毛片 午夜伊人网 | a级国产乱理伦片在线观看99 | 国产日韩欧美精品 | 玖草 | 欧美成人极品怡红院tv | japanese 色系 tube日本 | 日韩美女在线看免费观看 | 亚洲欧美日韩久久一区 | 国产亚洲综合久久 |