Skip to content

Commit 301265d

Browse files
docs: update Angular integration examples for modern APIs (#2781)
* docs: uptade Angular example to use modern angular features, like input and viewChild signals * docs: update Angular documentation to instruct how to use in version prior to 17.1 * docs(angular): correct signal-API version requirements viewChild() landed in 17.2 (not 17.1) and is stable only from Angular 19; input() is 17.1+ and stable from 19. Verified via tsc against @angular/core@17.1.0: 'has no exported member named viewChild'. Also aligns onReady log string with React/Vue docs. --------- Co-authored-by: Caio Pizzol <caio@harbourshare.com> Co-authored-by: Caio Pizzol <97641911+caio-pizzol@users.noreply.github.com>
1 parent cc0a336 commit 301265d

1 file changed

Lines changed: 36 additions & 30 deletions

File tree

apps/docs/getting-started/frameworks/angular.mdx

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ keywords: "angular docx editor, angular word component, superdoc angular, angula
66

77
SuperDoc works with Angular through direct DOM manipulation. No wrapper needed.
88

9+
Requires Angular 17.2+. `viewChild()` and `input()` are stable from Angular 19; developer preview before that. On older versions, use `@ViewChild`, `@Input`, and `ngOnDestroy` in place of `inject(DestroyRef)`.
10+
911
## Install
1012

1113
```bash
@@ -15,29 +17,29 @@ npm install superdoc
1517
## Basic setup
1618

1719
```typescript
18-
import { Component, ElementRef, ViewChild, OnInit, OnDestroy } from '@angular/core';
20+
import { Component, ElementRef, viewChild, AfterViewInit, inject, DestroyRef } from '@angular/core';
1921
import { SuperDoc } from 'superdoc';
2022
import 'superdoc/style.css';
2123

2224
@Component({
2325
selector: 'app-editor',
2426
template: `<div #editor style="height: 700px"></div>`,
2527
})
26-
export class EditorComponent implements OnInit, OnDestroy {
27-
@ViewChild('editor', { static: true }) editorRef!: ElementRef;
28+
export class EditorComponent implements AfterViewInit {
29+
private readonly editorRef = viewChild.required<ElementRef<HTMLDivElement>>('editor');
2830
private superdoc: SuperDoc | null = null;
2931

30-
ngOnInit() {
32+
constructor() {
33+
inject(DestroyRef).onDestroy(() => this.superdoc?.destroy());
34+
}
35+
36+
ngAfterViewInit() {
3137
this.superdoc = new SuperDoc({
32-
selector: this.editorRef.nativeElement,
38+
selector: this.editorRef().nativeElement,
3339
document: 'contract.docx',
3440
documentMode: 'editing',
3541
});
3642
}
37-
38-
ngOnDestroy() {
39-
this.superdoc?.destroy();
40-
}
4143
}
4244
```
4345

@@ -46,7 +48,7 @@ export class EditorComponent implements OnInit, OnDestroy {
4648
A reusable editor component with mode switching and export:
4749

4850
```typescript
49-
import { Component, ElementRef, ViewChild, Input, OnInit, OnDestroy } from '@angular/core';
51+
import { Component, ElementRef, viewChild, input, AfterViewInit, inject, DestroyRef } from '@angular/core';
5052
import { SuperDoc } from 'superdoc';
5153
import 'superdoc/style.css';
5254

@@ -62,27 +64,27 @@ import 'superdoc/style.css';
6264
<div #editor style="height: 700px"></div>
6365
`,
6466
})
65-
export class DocEditorComponent implements OnInit, OnDestroy {
66-
@ViewChild('editor', { static: true }) editorRef!: ElementRef;
67-
@Input() document!: File | string;
68-
@Input() user?: { name: string; email: string };
67+
export class DocEditorComponent implements AfterViewInit {
68+
private readonly editorRef = viewChild.required<ElementRef<HTMLDivElement>>('editor');
69+
readonly document = input.required<File | string>();
70+
readonly user = input<{ name: string; email: string }>();
6971

7072
private superdoc: SuperDoc | null = null;
7173

72-
ngOnInit() {
74+
constructor() {
75+
inject(DestroyRef).onDestroy(() => this.superdoc?.destroy());
76+
}
77+
78+
ngAfterViewInit() {
7379
this.superdoc = new SuperDoc({
74-
selector: this.editorRef.nativeElement,
75-
document: this.document,
80+
selector: this.editorRef().nativeElement,
81+
document: this.document(),
7682
documentMode: 'editing',
77-
user: this.user,
78-
onReady: () => console.log('Editor ready'),
83+
user: this.user(),
84+
onReady: () => console.log('Editor ready!'),
7985
});
8086
}
8187

82-
ngOnDestroy() {
83-
this.superdoc?.destroy();
84-
}
85-
8688
setMode(mode: 'editing' | 'viewing' | 'suggesting') {
8789
this.superdoc?.setDocumentMode(mode);
8890
}
@@ -96,32 +98,36 @@ export class DocEditorComponent implements OnInit, OnDestroy {
9698
## Handle file uploads
9799

98100
```typescript
101+
import { Component, ElementRef, viewChild, inject, DestroyRef } from '@angular/core';
102+
import { SuperDoc } from 'superdoc';
103+
import 'superdoc/style.css';
104+
99105
@Component({
100106
selector: 'app-upload-editor',
101107
template: `
102108
<input type="file" accept=".docx" (change)="onFileChange($event)" />
103109
<div #editor style="height: 700px"></div>
104110
`,
105111
})
106-
export class UploadEditorComponent implements OnDestroy {
107-
@ViewChild('editor', { static: true }) editorRef!: ElementRef;
112+
export class UploadEditorComponent {
113+
private readonly editorRef = viewChild.required<ElementRef<HTMLDivElement>>('editor');
108114
private superdoc: SuperDoc | null = null;
109115

116+
constructor() {
117+
inject(DestroyRef).onDestroy(() => this.superdoc?.destroy());
118+
}
119+
110120
onFileChange(event: Event) {
111121
const file = (event.target as HTMLInputElement).files?.[0];
112122
if (!file) return;
113123

114124
this.superdoc?.destroy();
115125
this.superdoc = new SuperDoc({
116-
selector: this.editorRef.nativeElement,
126+
selector: this.editorRef().nativeElement,
117127
document: file,
118128
documentMode: 'editing',
119129
});
120130
}
121-
122-
ngOnDestroy() {
123-
this.superdoc?.destroy();
124-
}
125131
}
126132
```
127133

0 commit comments

Comments
 (0)