Created
June 1, 2015 18:16
-
-
Save rheid/6f2e8aaf07fa44bb2eac to your computer and use it in GitHub Desktop.
AngularJS Typed Script Provider
This file contains 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
// Interface describing the members that the provider's service offers | |
interface IGreetingService { | |
getGreeting(): string; | |
} | |
// The following class represents the provider | |
class GreetingService implements ng.IServiceProvider { | |
private greeting = "Hello World!"; | |
// Configuration function | |
public setGreeting(greeting: string) { | |
this.greeting = greeting; | |
} | |
// Provider's factory function | |
public $get() : IGreetingService { | |
return { | |
getGreeting: () => { return this.greeting; } | |
}; | |
} | |
} | |
// Define a controller depending our provider | |
class ControllerNeedingProvider { | |
constructor($scope, GreetingService: IGreetingService) { | |
$scope.Greeting = GreetingService.getGreeting(); | |
} | |
} | |
angular.module("ProviderApp", []) | |
// Define provider | |
.provider("GreetingService", GreetingService) | |
// Configure provider (note the suffix "Provider" here) | |
.config((GreetingServiceProvider: GreetingService) => { | |
GreetingServiceProvider.setGreeting("Hello Provider"); | |
}) | |
.controller("ControllerNeedingProvider", ControllerNeedingProvider); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment