Components break up the application into smaller parts; whereas, Directives add behavior to an existing DOM element.
@Input
and @Output
directives?ngOnChanges()
- Respond when Angular (re)sets data-bound input properties. The method receives a SimpleChanges object of current and previous property values.
Called before ngOnInit()
and whenever one or more data-bound input properties change.ngOnInit()
- Initialize the directive/component after Angular first displays the data-bound properties and sets the directive/component’s input properties. Called once, after the first ngOnChanges()
.ngDoCheck()
- Detect and act upon changes that Angular can’t or won’t detect on its own. Called during every change detection run, immediately after ngOnChanges() and ngOnInit()
.ngAfterContentInit()
- Respond after Angular projects external content into the component’s view / the view that a directive is in. Called once after the first ngDoCheck()
.ngAfterContentChecked()
- Respond after Angular checks the content projected into the directive/component. Called after the ngAfterContentInit() and every subsequent ngDoCheck()
.ngAfterViewInit()
- Respond after Angular initializes the component’s views and child views / the view that a directive is in. Called once after the first ngAfterContentChecked()
.ngAfterViewChecked()
- Respond after Angular checks the component’s views and child views / the view that a directive is in. Called after the ngAfterViewInit and every subsequent ngAfterContentChecked()
.ngOnDestroy()
- Cleanup just before Angular destroys the directive/component. Unsubscribe Observables and detach event handlers to avoid memory leaks. Called just before Angular destroys the directive/component.<p>this is a paragraph</p>
and <h1>this is a heading</h1>
.<app-tax>some tax-related content</app-tax>
This will not work the way it worked for HTML elements.<ng-content>
tag directive is used.Emulated
: styles from other HTML spread to the component.Native
: styles from other HTML do not spread to the component.None
: styles defined in a component are visible to all components.@HostBinding('class.valid') isValid;
Binds a host element property (here, the CSS class valid) to a directive/component property (isValid).
A Dumb Component is a component that works like a pure function. (A pure function is a function that for given function arguments, will always produce the same return value.) A Dumb Component is just like that. It is a component that for received data (inputs), will always look and behave the same, possibly also producing other data (events, via outputs).
A Smart Component is a component that is more like an impure function.
(An impure function is a function that touches “the outer world”: either by obtaining data from external services or by producing side effects.)
A Smart Component is just like that. It is not only dependant on its’ inputs, but also on some kind of external data (“the outer world”), which is not passed directly via @Input()
.
It might also produce some side effects that are not emitted through the @Output()
interface.
For example, a component which gets current user data from a singleton service instantiated elsewhere; from an external API; or from LocalStorage. A component that changes the state of an external service;
issues an API call; or changes the stored data in LocalStorage.
See here for more details.
Types of communication:
Virtual DOM is about avoiding unnecessary changes to the DOM, which are expensive performance-wise, because changes to the DOM usually cause re-rendering of the page. Virtual DOM also allows to collect several changes to be applied at once, so not every single change causes a re-render, but instead re-rendering only happens once after a set of changes was applied to the DOM.
A single custom element can implement more-or-less complex logic combined with more-or-less complex DOM. An entire web application of arbitrary complexity can be added to a page by an import and <my-app></my-app>
but also simpler reusable and composable components can be implemented as custom elements where the internal representation is hidden in the shadow DOM like <date-picker></date-picker>
.