Using decorators
Decorators are functions that modify a class, property, method, or method parameter. The following example illustrates how to define and use a simple decorator that adds a static parameter to the class:
// decorator function function AddMetadata (...args) { return function (target){ target.metadata = [...args]; } } // decorator applied @AddMetadata({ metadata: 'some values'}) class Model { }
The three dots syntax (...
) is the spread operator, which is a feature of JavaScript 2015 that deconstructs the items of a given array.
Decorators versus annotations
You might have heard the term annotations; they are simply metadata related to Angular 2. Before the Angular team decided to use TypeScript, they introduced us to a new language that ...
Get Angular 2 Components now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.