av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

Angular組件庫ng-zorro-antd實現radio單選框選擇

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

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

項目業務之前的代碼設計了類似radio單選框取消選擇的相關邏輯,用的是下面類似的代碼實現的。但近期對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打斷點調試

  • 將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,且組件內init時都有對selected$這個流做監聽
@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();
      });
  • 當監聽完值改變后,后面又執行了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 使組件庫內部先執行完click后續再執行。

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 = ""
    })
  }
}
}

總結

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

標簽: JavaScript
主站蜘蛛池模板: 观看av | 亚洲 欧美 综合 | 成人久久久 | 国产激情一区二区三区 | 欧美在线国产精品 | 手机av在线 | 国产色片在线 | 国产成人99久久亚洲综合精品 | 色本道 | 成人一区二区在线 | 日韩欧美黄色 | 成人字幕网zmw| 亚洲欧洲视频 | 天天爽一爽 | 日韩精品免费在线观看 | 欧美成人a∨高清免费观看 老司机午夜性大片 | 在线一区二区三区 | 99精品国自产在线 | 欧美日韩精品区 | 一级欧美视频 | 在线观看亚洲精品 | 国产免费一区二区 | 最新中文字幕一区 | 日本高清aⅴ毛片免费 | 成人乱人乱一区二区三区软件 | 综合国产在线 | 国产精品一二三区在线观看 | 精品久久中文字幕 | 超碰电影 | 久久国产精品一区二区三区 | 国产免费高清 | 99精品久久久久久中文字幕 | 日韩一二三区视频 | 国产一区日韩在线 | 欧洲毛片| 中文字幕黄色大片 | 欧美性视频在线播放 | 国产精品久久久久久久久免费软件 | 欧美在线日韩 | 日韩精品久久一区 | 欧美a在线 |