Replacing Services with TypeScript Classes
Early AngularJS applications predate the widespread use of module loaders. The strategy of this era was to concatenate source files and rely on AngularJS's dependency injection as a poor-man's module loader. Often services were used to house libraries instead of stateful services.
During conversion, we will introduce Webpack as a module loader. For services that lack state and don't heavily rely on other dependency injected services, we recommend rewriting them using TypeScript modules. The advantages of writing code this way are:
It becomes framework agnostic (doesn't rely on AngularJS explicitly)
It is easier to test
It instantly works with both AngularJS and Angular
Even services that depend on a limited set of AngularJS services (e.g. $http
) can be rewritten by depending on other libraries (e.g. window.fetch
).
How do we get there?
Convert services using
.factory
to.service
Angular's
@Injectable
expects an object it can usenew
with,similar to how
.service
works (e.g.new CalculatorService()
)
Replace constructor functions with classes using the
class
keyword.Use the class directly by
export
ing it.
Example
.factory
original
.factory
originalConversion to .service
.service
Conversion to TypeScript class
Also add type information to all methods to make the services safer and easier to use.
Skip the middleman
Last updated