Skip to content

Commit f2b47d1

Browse files
Angular: add info about standalone and directives (DevExpress#6970) (DevExpress#6975)
1 parent 647f4a0 commit f2b47d1

File tree

2 files changed

+79
-8
lines changed

2 files changed

+79
-8
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
To change attributes of all components of one type in an app, use [defaultOptions](/Documentation/ApiReference/UI_Components/dxTextBox/Methods/#defaultOptionsrule). To change attributes of multiple components, use directives.
2+
3+
When working with [custom directives](https://angular.dev/guide/directives/attribute-directives), follow these steps:
4+
5+
1. Import required components in the `.directive.ts` file. For example, `import { DxButtonComponent } from 'devextreme-angular';`.
6+
2. Define the components in the constructor. Use the [`@Host`](https://angular.dev/api/core/Host?tab=usage-notes) decorator to access the instance of hosting components. Use the [`@Optional`](https://angular.dev/api/core/Optional?tab=usage-notes) decorator when specifying multiple components.
7+
3. Use the `ngOnViewInit()` hook to ensure component initialization.
8+
9+
The following code snippet creates a directive that changes the style and switches "apple" to "banana" in the TextArea and TextBox components:
10+
11+
<!-- tab: replace.directive.ts -->
12+
import { Directive, Host, Optional } from '@angular/core';
13+
import { DxTextBoxComponent, DxTextAreaComponent } from 'devextreme-angular';
14+
import { DxTextAreaTypes } from 'devextreme-angular/ui/text-area';
15+
import { DxTextBoxTypes } from 'devextreme-angular/ui/text-box';
16+
17+
@Directive({
18+
selector: '[appReplace]',
19+
standalone: true,
20+
})
21+
export class ReplaceDirective {
22+
component: any;
23+
24+
constructor(
25+
@Optional() @Host() textBox: DxTextBoxComponent,
26+
@Optional() @Host() textArea: DxTextAreaComponent
27+
) {
28+
this.component = textBox || textArea;
29+
}
30+
31+
replaceApple(e: DxTextAreaTypes.FocusOutEvent & DxTextBoxTypes.FocusOutEvent) {
32+
const value = e.component.option('value');
33+
e.component.option('value', value?.replace(/apple/g, 'banana'));
34+
}
35+
36+
ngAfterViewInit() {
37+
this.component.instance.option('stylingMode', 'filled');
38+
this.component.instance.on('focusOut', this.replaceApple);
39+
}
40+
41+
ngOnDestroy() {
42+
this.component.instance.off('focusOut', this.replaceApple);
43+
}
44+
}
45+
46+
<!-- tab: app.component.ts -->
47+
import { Component } from '@angular/core';
48+
import { DxTextBoxModule, DxTextAreaModule } from 'devextreme-angular';
49+
import { ReplaceDirective } from './replace.directive';
50+
51+
@Component({
52+
selector: 'app-root',
53+
standalone: true,
54+
imports: [DxTextBoxModule, DxTextAreaModule, ReplaceDirective],
55+
templateUrl: './app.component.html',
56+
styleUrl: './app.component.css'
57+
})
58+
export class AppComponent { }
59+
60+
<!-- tab: app.component.html -->
61+
<dx-text-area appReplace></dx-text-area>
62+
<dx-text-box appReplace></dx-text-box>

includes/ng-import-devextreme-modules.md

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
1-
Go to the `NgModule` in which you are going to use DevExtreme UI components and import the required DevExtreme modules. Note that if [tree shaking](/concepts/40%20Angular%20Components/40%20Common%20Features/10%20Tree%20Shaking.md '/Documentation/Guide/Angular_Components/Common_Features/Tree_Shaking/') is configured in your application, you can import the modules from `devextreme-angular`. Otherwise, you should import them from specific files.
1+
If you use [standalone](https://angular.dev/guide/components) components, import the modules as shown below:
2+
3+
<!-- tab: app.component.ts -->
4+
import { Component } from '@angular/core';
5+
import { DxButtonModule } from 'devextreme-angular';
6+
7+
@Component({
8+
selector: 'app-root',
9+
standalone: true,
10+
imports: [DxButtonModule],
11+
templateUrl: './app.component.html',
12+
styleUrl: './app.component.css'
13+
})
14+
export class AppComponent { }
15+
16+
If you use `NgModule`, import the DevExtreme modules as demonstrated in the following code snippet. If [tree shaking](/concepts/40%20Angular%20Components/40%20Common%20Features/10%20Tree%20Shaking.md '/Documentation/Guide/Angular_Components/Common_Features/Tree_Shaking/') is configured in your application, import the modules from `devextreme-angular`. If not, import them from files.
217

318
<!-- tab: app.module.ts -->
419
// ...
@@ -24,13 +39,7 @@ Now you can use the DevExtreme UI component in your application:
2439
</dx-button>
2540

2641
<!-- tab: app.component.ts -->
27-
import { Component } from '@angular/core';
28-
29-
@Component({
30-
selector: 'app-root',
31-
templateUrl: './app.component.html',
32-
styleUrls: ['./app.component.css']
33-
})
42+
// ...
3443
export class AppComponent {
3544
helloWorld() {
3645
alert('Hello world!');

0 commit comments

Comments
 (0)