ECMAScript7 decorators for core Angular1.x providers
- @Module
- @Requires
- @App
- @Inject
- @Service
- @Resource
- @ServiceFactory
- @Controller
- @Factory
- @Filter
- @ClassFactory
- @Directive
- @Component
- @Page
- @DirectiveFactory
- @Provider
- @ProviderFactory
- @Constant
- @Value
- @Config
- @Run
Declare angular module with given name.
Note:
@Requires
decorator has to be put next line to the@Module
.ng.IModule
instance will be passed to constructor.
@Module('Database')
class DatabaseModule {
constructor(
module: ng.IModule
) {}
}
var DatabaseModule = angular.module('Database', [])
Define module or provider injection requirements.
@Module('Database')
@Requires('ngResource', 'ngStorage')
class DatabaseModule {
constructor(
module: ng.IModule
) {}
}
var DatabaseModule = angular.module('Database', ['ngResource', 'ngStorage'])
@Service('DbService')
@Requires('$localStorate', '$resource')
class DbService {
constructor(
private $localStorate: {},
private $resource: ng.resource.IResourceService
) {}
}
var DatabaseModule = angular.module('Database', ['ngResource', 'ngStorage'])
function DbService ($localStorate, $resource) {
}
DbService.$inject = ['$localStorate', '$resource'];
DatabaseModule.service('dbService', DbService)
Declare angular module with given name.
Note:
@Requires
decorator should be put next line to the@App
.- If module already defined it will be used to bootstrap aplication.
ng.IModule
instance will be passed to constructor.- If
window.$bootstrap
defined waits fot it's resolving to start bootstraping.
@App(angular.element('body'), 'Application')
@Requires('ngResource', 'ngStorage')
class Application {
constructor(
module: ng.IModule
) {}
}
@App(angular.element('body'))
@Module('Application')
@Requires('ngResource', 'ngStorage')
class Application {
constructor(
module: ng.IModule
) {}
}
var Application = angular.module('Application', ['ngResource', 'ngStorage'])
angular.element(document).ready(function () {
angular.bootstrap(angular.element('body'), ['Application'])
})
Define parameter injection to constructor or function.
@Service(DatabaseModule, 'DbService')
class DbService {
constructor(
@Inject('$localStorate') private $localStorate: {},
@Inject('$resource') private $resource: ng.resource.IResourceService
) {}
}
class DbServiceFactory {
@ServiceFactory(DatabaseModule, 'DbService')
public static DbServiceFactory(
@Inject('$localStorate') $localStorate: {},
@Inject('$resource') $resource: ng.resource.IResourceService
) {}
}
function DbService ($localStorate, $resource) {
}
DbService.$inject = ['$localStorate', '$resource'];
DatabaseModule.service('dbService', DbService)
Declare angular service with decorated class. New instance of service decorated class will be instantiated once.
@Service(DatabaseModule, 'DbService')
class DbService {
constructor(
@Inject('$localStorate') private $localStorate: {},
@Inject('$resource') private $resource: ng.resource.IResourceService
) {}
}
function DbService ($localStorate, $resource) {
}
DbService.$inject = ['$localStorate', '$resource'];
DatabaseModule.service('dbService', DbService)
Declare angular service with decorated factory method.
class DbServiceFactory {
@ServiceFactory(DatabaseModule, 'DbService')
public static DbServiceFactory(
@Inject('$localStorate') $localStorate: {},
@Inject('$resource') $resource: ng.resource.IResourceService
) {}
}
function DbService ($localStorate, $resource) {
}
DbService.$inject = ['$localStorate', '$resource'];
DatabaseModule.service('dbService', DbService)
Declares ngResource service $resource.
Will user Angular cached resource if such provider registered.
Otherwise will create new service using $resource
provider.
@Resource(Application, 'UsersResource', '/api/users/:id', {
id: '@id
}, {
})
class UsersResource {
constructor(
@Inject('$q') private $q: ng.IQService
) {
}
public fullName(): ng.IPromise<string> {
return this.$q.resolve(`${this.firstName} ${this.lastName}`);
}
}
Declare angular with decorated class.
@Controller(Application, 'AppController')
class AppController {
constructor(
@Inject('$localStorate') private $localStorate: {},
@Inject('$resource') private $resource: ng.resource.IResourceService
) {}
}
function AppController ($localStorate, $resource) {
}
AppController.$inject = ['$localStorate', '$resource'];
Application.service('AppController', AppController)
Declare angular factory as factory method.
class ConnectionFactory {
@Factory(Application, 'Connection')
public static ConnectionFactory(
@Inject('$localStorate') $localStorate: {},
@Inject('$resource') $resource: ng.resource.IResourceService
) {}
}
function ConnectionFactory ($localStorate, $resource) {
}
ConnectionFactory.$inject = ['$localStorate', '$resource'];
Application.factory('Connection', ConnectionFactory)
Declare angular filter factory with decorated factory method.
class TimeFilterFactory {
@Filter(Application, 'TimeFilter')
public static TimeFilter(
@Inject('$window') $window: ng.IWindow
) {}
}
function TimeFilter ($window) {
}
TimeFilter.$inject = ['$window'];
Application.filter('TimeFilter', TimeFilter)
Declare angular factory with decorated class. New instance of factory decorated class will be instantiated for each injection.
@ClassFactory(Application, 'Connection')
class Connection {
constructor(
@Inject('$localStorate') private $localStorate: {},
@Inject('$resource') private $resource: ng.resource.IResourceService
) {}
}
function ConnectionFactory ($localStorate, $resource) {
}
ConnectionFactory.$inject = ['$localStorate', '$resource'];
Application.factory('Connection', ConnectionFactory)
Declare angular directive with decorated class as controller.
@Directive(Application, 'appComponent', {
restrict: 'E',
contollerAs: 'ctrl'
template: '<div>Hello, {{ctrl.who}}!</div>'
})
class AppComponentController {
constructor(
@Inject('$scope') private $scope: ng.IScope
) {}
}
function AppComponentController ($scope) {
}
AppComponentController.$inject = ['$scope'];
Application.controller('AppComponentController', ConnectionFactory)
Application.directive('appComponent', function () {
return {
restrict: 'E',
controller: AppComponentController,
contollerAs: 'ctrl'
template: '<div>Hello, {{ctrl.who}}!</div>'
}
})
Declare angular directive with decorated factory method.
class AppComponentFactory {
@DirectiveFactory(Application, 'appComponent', {
restrict: 'E',
contollerAs: 'ctrl'
template: '<div>Hello, {{ctrl.who}}!</div>'
})
public static AppComponentFactory(
@Inject('$scope') $scope: ng.IScope
) {
return {
restrict: 'E',
template: '<div>Hello, World!</div>'
}
}
}
Application.directive('appComponent', function () {
return {
restrict: 'E',
template: '<div>Hello, World!</div>'
}
})
Declare angular directive with decorated class as controller.
@Component(Application, 'appComponent', {
template: '<div>Hello, {{$ctrl.who}}!</div>'
})
class AppComponentController {
constructor(
@Inject('$scope') private $scope: ng.IScope
) {}
}
function AppComponentController ($scope) {
}
AppComponentController.$inject = ['$scope'];
Application.component('appComponent', {
controller: AppComponentController,
template: '<div>Hello, {{$ctrl.who}}!</div>'
})
Declare angular service provider with decorated class. New instance of provider decorated class will be instantiated once.
@Provider(DatabaseModule, 'DbProvider')
class DbProvider implements ng.IServiceProviderFactory {
constructor(
@Inject('$locationProvider') private $locationProvider: ng.ILocationProvider
) {}
public $get() {
return this; // or anything else
}
}
function DbProvider ($locationProvider) {
}
DbProvider.prototype.$get = function () {
return this;
}
DbProvider.$inject = ['$localStorate', '$resource'];
DatabaseModule.provider('DbProvider', DbProvider)
Declare angular service provider with decorated factory method.
class DbProvider implements ng.IServiceProviderFactory {
@ProviderFactory(DatabaseModule, 'DbProvider')
public static DbProviderFactory(
@Inject('$locationProvider') $locationProvider: ng.ILocationProvider
) {
return {
$get: function () {
return 0; // or anything else
}
}
}
}
function DbProvider ($locationProvider) {
}
DbProvider.prototype.$get = function () {
return this;
}
DbProvider.$inject = ['$localStorate', '$resource'];
DatabaseModule.provider('DbProvider', DbProvider)
Declare angular constant provider with decorated class. Injections are unavailable for this type of providers.
@Constant(DatabaseModule, 'DbCredentilas')
class DbCredentilas {
public connectionString = 'mongodb://...';
}
function DbCredentilas () {
this.connectionString = 'mongodb://...'
}
DatabaseModule.constant('DbCredentilas', new DbCredentilas())
Declare angular value provider with decorated class. Injections are unavailable for this type of providers.
@Value(DatabaseModule, 'DbCredentilas')
class DbCredentilas {
public connectionString = 'mongodb://...';
}
function DbCredentilas () {
this.connectionString = 'mongodb://...'
}
DatabaseModule.value('DbCredentilas', new DbCredentilas())
Declare angular config clause with decorated class. New instance of decorated class will be instantiated inside config clause.
@Config(DatabaseModule)
class DbConfig {
constructor(
@Inject('DbCredentilas') private credentials: any
) {
this.credentials; // this.constrctor === DbConfig
}
}
DatabaseModule.config(function (DbCredentilas) {
})
Declare angular run clause with decorated class. New instance of decorated class will be instantiated inside run clause.
@Run(DatabaseModule)
class DbConfig {
constructor(
@Inject('DbCredentilas') private credentials: any
) {
this.credentials; // this.constrctor === DbConfig
}
}
DatabaseModule.run(function (DbCredentilas) {
})
/dist/decorators.js
file oplimized for usage with SystemJS modularizer.
I use following trick to load all registered modules.
<script src="build/app.vendor.js"></script> <!-- can contains system.js and decorators.js -->
<script src="bower_components/system.js/dist/system.js"></script> <!-- can be included to concatenation of 3-p libs -->
<script src="bower_components/ng-decorators/dist/system/decorators.js"></script> <!-- can be included to concatenation of 3-p libs -->
<script src="build/app.bundle.js"></script>
<script src="entry.js"></script>
and this small js to start application
// or any other Promise
var $injector = angular.injector(['ng']);
var $q = $injector.get('$q');
var imports = [];
for (var defined in System.defined) {
imports.push(System.import(defined));
}
// used to ensure all modules are loaded before call angular.bootstrap(...)
window.$bootstrap = $q.all(imports)
.catch(function (err) {
console.error(err);
throw err;
});
The MIT License (MIT)
Copyright (c)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.