Angular Decorators: @ngmodule
Angular Decorators: @ngmodule
Before we look at creating a custom decorator and why/how Angular uses them, let’s look at the
different types of decorators that Angular offers. There are four main types:
@Directive({...})
class MyDirective() {}
Declares that a class is a directive and provides metadata about the directive.
@Pipe({...})
class MyPipe() {}
Declares that a class is a pipe and provides metadata about the pipe.
@Injectable()
class MyService() {}
Declares that a class has dependencies that should be injected into the constructor when the
dependency injector is creating an instance of this class.
cmp [myProperty]="someExpression">).
(myEvent)="doSomething()">).
property (isValid).
($event).
@component:
https://docs.google.com/document/d/1ale3W25RZYmXh4rVSjagC01pg6Plw600s-
b3WZhBMOo/edit
@Directive:
https://docs.google.com/document/d/1rm9hgZWN6LQ_JlW9pGfby50QsFXAyeEnHnHEy-
svNdI/edit?usp=sharing
@input:
npm Package: @angular/core
Module: import { Input } from '@angular/core';
Source: core/src/metadata/directives.ts
@Input({
bindingPropertyName?: string
})
@output:
npm Package: @angular/core
@viewChild:
https://angular.io/api/core/ViewChild
See here:
The local variable approach is simple and easy. But it is limited because the parent-child wiring must
be done entirely within the parent template. The parent component itself has no access to the child.
You can't use the local variable technique if an instance of the parent component class must read or
When the parent component class requires that kind of access, inject the child component into the
parent as a ViewChild.
How To Use:
import {AfterViewInit, Component, Directive, ViewChild} from '@angular/core';
1.
2. @Directive({selector: 'child-directive'})
3. class ChildDirective {
4. }
5.
9.
10. ngAfterViewInit() {
12. }
13. }
Description: You can use ViewChild to get the first element or the directive matching the
selector from the view DOM. If the view DOM changes, and a new child matches the selector,
the property will be updated.
Metadata Properties:
content_copy
2.
3. @Directive({selector: 'pane'})
6. }
7.
8. @Component({
9. selector: 'example-app',
10. template: `
13.
15.
17. `,
18. })
20. @ViewChild(Pane)
23. }
27. }
@viewChildren: You can use ViewChildren to get the QueryList of elements or directives from the
view DOM. Any time a child element is added, removed, or moved, the query list will be updated, and
the changes observable of the query list will emit a new value.
View queries are set before the ngAfterViewInit callback is called.
@ContentChild:
npm Package: @angular/core
Module: import { ContentChild } from '@angular/core';
Source: core/src/metadata/di.ts
How To Use:
import {AfterContentInit, ContentChild, Directive} from '@angular/core';
1.
2. @Directive({selector: 'child-directive'})
3. class ChildDirective {
4. }
5.
6. @Directive({selector: 'someDir'})
9.
10. ngAfterContentInit() {
12. }
13. }
Description: You can use ContentChild to get the first element or the directive matching the
selector from the content DOM. If the content DOM changes, and a new child matches the
selector, the property will be updated.
Content queries are set before the ngAfterContentInit callback is called.
Metadata Properties:
2.
3. @Directive({selector: 'pane'})
6. }
7.
8. @Component({
9. selector: 'tab',
10. template: `
12. `
13. })
16. }
17.
18. @Component({
20. template: `
21. <tab>
24. </tab>
25.
27. `,
28. })
31.
33. }
@ContentChildren:
How To Use:
import {AfterContentInit, ContentChildren, Directive, QueryList} from '@angular/core';
1.
2. @Directive({selector: 'child-directive'})
3. class ChildDirective {
4. }
5.
6. @Directive({selector: 'someDir'})
9.
10. ngAfterContentInit() {
12. }
13. }
Description: You can use ContentChildren to get the QueryList of elements or directives from
the content DOM. Any time a child element is added, removed, or moved, the query list will be
updated, and the changes observable of the query list will emit a new value.
Metadata Properties:
● @Directive({selector: 'pane'})
● export class Pane {
● }
● @Component({
● selector: 'tab',
● template: `
● `
● })
● }
'';
● }
● }
● @Component({
● selector: 'example-app',
● template: `
● <tab>
● <pane id="1"></pane>
● <pane id="2"></pane>
● <tab>
● <pane id="3_1"></pane>
● <pane id="3_2"></pane>
● </tab>
● </pane>
● </tab>
● `,
● })
● shouldShow = false;
● }
@ViewChildren look for elements in Shadow DOM while @ContentChildren look for them
in Light DOM.
https://stackoverflow.com/questions/34326745/whats-the-difference-between-viewchild-and-
contentchild
https://ng2.codecraft.tv/components/viewchildren-and-
contentchildren/#_viewchild_referencing_a_template_local_variable