|
| 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> |
0 commit comments