From e9753cb43c392a5807319813c4db501701a72dc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=A1=E8=89=B2?= Date: Mon, 18 Mar 2024 15:05:03 +0800 Subject: [PATCH] fix(abc:cell): fix can't change detection of widget (#1787) --- packages/abc/cell/cell-host.directive.ts | 12 ++++++------ packages/abc/cell/cell.spec.ts | 1 + packages/abc/cell/demo/simple.md | 12 ++++++++---- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/packages/abc/cell/cell-host.directive.ts b/packages/abc/cell/cell-host.directive.ts index 5f226909b8..5ebbccfb89 100644 --- a/packages/abc/cell/cell-host.directive.ts +++ b/packages/abc/cell/cell-host.directive.ts @@ -1,4 +1,4 @@ -import { Directive, Input, OnInit, Type, ViewContainerRef, inject } from '@angular/core'; +import { Directive, Input, OnChanges, Type, ViewContainerRef, inject } from '@angular/core'; import { warn } from '@delon/util/other'; @@ -9,13 +9,13 @@ import { CellTextResult } from './cell.types'; selector: '[cell-widget-host]', standalone: true }) -export class CellHostDirective implements OnInit { +export class CellHostDirective implements OnChanges { private readonly srv = inject(CellService); - private readonly viewContainerRef = inject(ViewContainerRef); + private readonly vcr = inject(ViewContainerRef); @Input() data!: CellTextResult; - ngOnInit(): void { + ngOnChanges(): void { const widget = this.data.options.widget!; const componentType = this.srv.getWidget(widget.key!)?.ref as Type; if (componentType == null) { @@ -25,8 +25,8 @@ export class CellHostDirective implements OnInit { return; } - this.viewContainerRef.clear(); - const componentRef = this.viewContainerRef.createComponent(componentType); + this.vcr.clear(); + const componentRef = this.vcr.createComponent(componentType); (componentRef.instance as { data: CellTextResult }).data = this.data; } } diff --git a/packages/abc/cell/cell.spec.ts b/packages/abc/cell/cell.spec.ts index a2163a177b..666780f92f 100644 --- a/packages/abc/cell/cell.spec.ts +++ b/packages/abc/cell/cell.spec.ts @@ -208,6 +208,7 @@ describe('abc: cell', () => { const srv = TestBed.inject(CellService); srv.registerWidget(TestWidget.KEY, TestWidget); page.update('1', { widget: { key: TestWidget.KEY, data: 'new data' } }).check('1-new data'); + page.update('1', { widget: { key: TestWidget.KEY, data: 'new data2' } }).check('1-new data2'); }); it('when key is invalid', () => { spyOn(console, 'warn'); diff --git a/packages/abc/cell/demo/simple.md b/packages/abc/cell/demo/simple.md index 77f2f0a390..7b95c1d377 100644 --- a/packages/abc/cell/demo/simple.md +++ b/packages/abc/cell/demo/simple.md @@ -143,10 +143,8 @@ import { NzGridModule } from 'ng-zorro-antd/grid';
Text Unit =>
custom widget => - + + Refresh Image
`, @@ -165,6 +163,7 @@ export class DemoComponent implements OnInit { private readonly ds = inject(DomSanitizer); private readonly cdr = inject(ChangeDetectorRef); value: unknown = 'string'; + imageValue = 'https://randomuser.me/api/portraits/thumb/women/47.jpg'; checkbox = false; radio = true; disabled = false; @@ -221,5 +220,10 @@ export class DemoComponent implements OnInit { this.safeHtml = this.ds.bypassSecurityTrustHtml(`alert('a');`); this.cdr.detectChanges(); } + + refreshImage(): void { + this.imageValue = `https://randomuser.me/api/portraits/thumb/women/${Math.floor(Math.random() * 50) + 10}.jpg`; + this.cdr.detectChanges(); + } } ```