Decorators for Angular (ngx-decorators)
Manage your Angular components like a boss.
NOTE! Decorators for Angular is not compatible with Angular AOT compilation.
Installation
Install using NPM CLI
npm install --save ngx-decorators
or using Yarn CLI
yarn add ngx-decorators
Use cases
Custom component decorator
Do you find yourself copying and pasting the same decorator configuration options for your components?
Create a custom component decorator and use it as a drop-in replacement for
Component
from @angular/core
. You only need to change the import
statement.
Customize defaults
The Angular component decorator defaults to emulated view encapsulation and the default change detection strategy.
Create a custom component decorator with no view encapsulation, native view
encapsulation (shadow DOM) and the OnPush
change detection strategy. Set
defaults or enforce your choices.
Build your own component options combinators
We have included helpers for managing change detection and view encapsulation as well as adding a common class to the host component.
Create your own component options combinators to manage white space preservation in component templates, add host element attributes, host styles or any other current or future component option.
Usage
Angular Material uses TSLint rules to enforce common component decorator options. Let us create a custom component decorator to enforce the common options.
;; ;;; ;
You can omit the types if you find it less distracting:
// src/app/decorators/component.decorator.ts;; ;;; ;
Now we are ready to use it in our project.
Before we had
(sample from Angular Material version 5)
;
Now we are left with
;
Even if we by accident add the default change detection strategy or emulated view encapsulation, our custom component decorator takes care of it for us. This is of course entirely customizable.
Look at the example app in this repository to see our various helpers in-use.
Supported Angular versions
Decorators for Angular has been tested with Angular versions 2 through 6. It
is unlikely that it will stop working until Angular's component decorator or the
public API of @angular/core
gets breaking changes.
Caveats
Angular AOT compilation
Angular AOT compilation does not support arrow functions or function expressions. While the arrow functions can easily be converted to function declarations, this library does not contribute anything without its combinator creators that are using function expressions.
Node.js
Do not use a custom component decorator for your root component, i.e.
AppComponent
unless you are using Node.js version 9 or newer.
Attributions
Thank you Mahmoud Abduljawad for your support, ideation and testing.