Skip to content

Instantly share code, notes, and snippets.

@jepetko
Created February 20, 2016 22:48
Show Gist options
  • Save jepetko/8cc60f48d8345c09fc98 to your computer and use it in GitHub Desktop.
Save jepetko/8cc60f48d8345c09fc98 to your computer and use it in GitHub Desktop.
demonstrates how to define angularjs providers in Typescript
//Example 1: inline definition... not configurable :-(
angular.module("myApp", [])
.provider("Dummy", <ng.IServiceProvider>{
'$get': function() {
return { value: 1 };
}
})
.config(["DummyProvider", function(Dummy: ng.IServiceProvider) {
}])
.run(["Dummy", function(Dummy: any) {
console.info(Dummy);
}]);
// Example 2: inline definition of a factory. Also not declared "typescript way". What if you would like to configure the value in the config phase?
angular.module("myApp", [])
.provider("Dummy", <ng.IServiceProviderFactory>function() {
return {
'$get': function() {
return {value: 1};
}
}
})
.config(["DummyProvider", function(Dummy: ng.IServiceProvider) {
}])
.run(["Dummy", function(Dummy: any) {
console.info(Dummy);
}]);
// Example 3: full configurable service because it's a class with a setter for the configurable property start.
class Dummy implements ng.IServiceProvider {
_start: number;
set start(start: number) {
this._start = start;
}
public $get(): any {
return {value: ++this._start};
}
}
angular.module('myApp', [])
.provider("Dummy", [() => {return new Dummy();}])
.config(["DummyProvider", function(DummyProvider: Dummy) {
DummyProvider.start = 10;
}])
.run(['Dummy', function(Dummy: Dummy) {
console.info(Dummy);
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment