-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
647f4a0
commit f2b47d1
Showing
2 changed files
with
79 additions
and
8 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
.../40 Angular Components/20 Component Configuration Syntax/90 Using Directives.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
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. | ||
|
||
When working with [custom directives](https://angular.dev/guide/directives/attribute-directives), follow these steps: | ||
|
||
1. Import required components in the `.directive.ts` file. For example, `import { DxButtonComponent } from 'devextreme-angular';`. | ||
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. | ||
3. Use the `ngOnViewInit()` hook to ensure component initialization. | ||
|
||
The following code snippet creates a directive that changes the style and switches "apple" to "banana" in the TextArea and TextBox components: | ||
|
||
<!-- tab: replace.directive.ts --> | ||
import { Directive, Host, Optional } from '@angular/core'; | ||
import { DxTextBoxComponent, DxTextAreaComponent } from 'devextreme-angular'; | ||
import { DxTextAreaTypes } from 'devextreme-angular/ui/text-area'; | ||
import { DxTextBoxTypes } from 'devextreme-angular/ui/text-box'; | ||
|
||
@Directive({ | ||
selector: '[appReplace]', | ||
standalone: true, | ||
}) | ||
export class ReplaceDirective { | ||
component: any; | ||
|
||
constructor( | ||
@Optional() @Host() textBox: DxTextBoxComponent, | ||
@Optional() @Host() textArea: DxTextAreaComponent | ||
) { | ||
this.component = textBox || textArea; | ||
} | ||
|
||
replaceApple(e: DxTextAreaTypes.FocusOutEvent & DxTextBoxTypes.FocusOutEvent) { | ||
const value = e.component.option('value'); | ||
e.component.option('value', value?.replace(/apple/g, 'banana')); | ||
} | ||
|
||
ngAfterViewInit() { | ||
this.component.instance.option('stylingMode', 'filled'); | ||
this.component.instance.on('focusOut', this.replaceApple); | ||
} | ||
|
||
ngOnDestroy() { | ||
this.component.instance.off('focusOut', this.replaceApple); | ||
} | ||
} | ||
|
||
<!-- tab: app.component.ts --> | ||
import { Component } from '@angular/core'; | ||
import { DxTextBoxModule, DxTextAreaModule } from 'devextreme-angular'; | ||
import { ReplaceDirective } from './replace.directive'; | ||
|
||
@Component({ | ||
selector: 'app-root', | ||
standalone: true, | ||
imports: [DxTextBoxModule, DxTextAreaModule, ReplaceDirective], | ||
templateUrl: './app.component.html', | ||
styleUrl: './app.component.css' | ||
}) | ||
export class AppComponent { } | ||
|
||
<!-- tab: app.component.html --> | ||
<dx-text-area appReplace></dx-text-area> | ||
<dx-text-box appReplace></dx-text-box> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters