|
| 1 | + |
| 2 | +// Import the core angular services. |
| 3 | +import { Component } from "@angular/core"; |
| 4 | +import { Directive } from "@angular/core"; |
| 5 | +import { Input } from "@angular/core"; |
| 6 | + |
| 7 | +// ----------------------------------------------------------------------------------- // |
| 8 | +// ----------------------------------------------------------------------------------- // |
| 9 | + |
| 10 | +@Component({ |
| 11 | + selector: "my-app", |
| 12 | + styleUrls: [ "./app.component.less" ], |
| 13 | + template: |
| 14 | + ` |
| 15 | + <p customProp="Testing [customProp]"> |
| 16 | + Super directive... |
| 17 | + </p> |
| 18 | + <p customProp2="Testing [customProp2]"> |
| 19 | + Sub directive... |
| 20 | + </p> |
| 21 | + ` |
| 22 | +}) |
| 23 | +export class AppComponent { |
| 24 | + // .... |
| 25 | +} |
| 26 | + |
| 27 | +// ----------------------------------------------------------------------------------- // |
| 28 | +// ----------------------------------------------------------------------------------- // |
| 29 | +/* |
| 30 | +@Directive({ |
| 31 | + selector: "[customProp]", |
| 32 | + // In the SUPER class, we're going to use the @Directive.inputs meta-data to tell |
| 33 | + // Angular that the class property, "customProp", maps to a template attribute of the |
| 34 | + // same name. |
| 35 | + inputs: [ |
| 36 | + "customProp" |
| 37 | + ] |
| 38 | +}) |
| 39 | +export class CustomPropDirective { |
| 40 | +
|
| 41 | + public customProp!: string; // Using definite assignment assertion. |
| 42 | +
|
| 43 | + // --- |
| 44 | + // PUBLIC METHODS. |
| 45 | + // --- |
| 46 | +
|
| 47 | + // I get called whenever one of the input bindings is changed. |
| 48 | + public ngOnChanges( c: any ) : void { |
| 49 | +
|
| 50 | + console.log( "Prop:", this.customProp ); |
| 51 | +
|
| 52 | + } |
| 53 | +
|
| 54 | +} |
| 55 | +
|
| 56 | +@Directive({ |
| 57 | + selector: "[customProp2]", |
| 58 | + // In the SUB class, we're going to use the @Directive.inputs meta-data to tell |
| 59 | + // Angular that the inherited property maps to a template attribute with a different |
| 60 | + // name, "customProp2". So, both classes will use the same internal class property; |
| 61 | + // but, they will be using two different template attributes. |
| 62 | + inputs: [ |
| 63 | + "customProp: customProp2" |
| 64 | + ] |
| 65 | +}) |
| 66 | +export class CustomProp2Directive extends CustomPropDirective { |
| 67 | + // .... |
| 68 | +} |
| 69 | +*/ |
| 70 | +// ----------------------------------------------------------------------------------- // |
| 71 | +// ----------------------------------------------------------------------------------- // |
| 72 | + |
| 73 | +@Directive({ |
| 74 | + selector: "[customProp]", |
| 75 | + // In the SUPER class, we're going to use the @Directive.inputs meta-data to tell |
| 76 | + // Angular that the class property, "customProp", maps to a template attribute of the |
| 77 | + // same name. |
| 78 | + inputs: [ |
| 79 | + "customProp" |
| 80 | + ] |
| 81 | +}) |
| 82 | +export class CustomPropDirective { |
| 83 | + |
| 84 | + public customProp!: string; // Using definite assignment assertion. |
| 85 | + |
| 86 | + // --- |
| 87 | + // PUBLIC METHODS. |
| 88 | + // --- |
| 89 | + |
| 90 | + // I get called whenever one of the input bindings is changed. |
| 91 | + public ngOnChanges( c: any ) : void { |
| 92 | + |
| 93 | + console.log( "Prop:", this.customProp ); |
| 94 | + |
| 95 | + } |
| 96 | + |
| 97 | +} |
| 98 | + |
| 99 | +@Directive({ |
| 100 | + selector: "[customProp2]" |
| 101 | +}) |
| 102 | +export class CustomProp2Directive extends CustomPropDirective { |
| 103 | + |
| 104 | + // Using @Input() meta-data in the sub-class WILL override the @Directive.inputs in |
| 105 | + // the super-class. |
| 106 | + @Input( "customProp2" ) |
| 107 | + public customProp!: string; // Using definite assignment assertion. |
| 108 | + |
| 109 | +} |
0 commit comments