Forked from jepetko/angular-typescript-providers.ts
Created
September 29, 2016 12:30
-
-
Save akhileshnirapure/f406a0ab52ccef6b147716dc81be5af3 to your computer and use it in GitHub Desktop.
demonstrates how to define angularjs providers in Typescript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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