Some Typescript decorators to ease maintenance/evolution of angularjs/Angular(>5) hybrid applications by removing a lot of boilerplate code:
-
@ng1Service()
: Makes an Angular service injectable in angularjs services/components -
@ng1Inject()
: Injects an angularjs service in Angular sevice/component -
@ng1Component()
: Makes an Angular component usable in angularjs templates
This micro library (5kb) is based on official recommendation.
Compatible with AOT and Ivy, tested with Angular 6/7/9/10 (although it might work with others)
See some examples below or the online demo or fork it on stackblitz
@Injectable()
@ng1Service('NewCatService')
export class NewCatService {
getNewSound(): string {
return "Meow meow meow";
}
}
angular
.module('demo.app')
.factory('CatService', CatService);
function CatService(NewCatService) {
var service = {
getSound: getSound,
}
return service;
function getSound() {
return NewCatService.getNewSound();
}
}
angular
.module('demo.app')
.factory('OldElephantService', function() {
var service = {
getSound: getSound,
}
return service;
function getSound() {
return 'Pwoa pwoa pwoaaa';
}
});
@Injectable()
export class ElephantService {
@ng1Inject() oldElephantService: any;
getSound(): string {
return this.oldElephantService.getSound();
}
}
@ng1Component('newGiraffe')
@Component({
selector: 'new-giraffe',
templateUrl: './new-giraffe.component.html',
})
export class NewGiraffeComponent {
@Input() sound!: string;
}
angular
.module('demo.app')
.directive('giraffe', giraffe);
function giraffe() {
var directive = {
restrict: 'E',
template: `<new-giraffe [sound]="'Bleat bleat bleat'"></new-giraffe>`,
controller: 'GiraffeController',
controllerAs: 'GiraffeCtrl',
};
return directive;
}
See also the online demo or fork it on stackblitz
npm i ng-hybrid-decorators
NgHybridService
needs to be called where the hybrid app is configured:
export class AppModule {
constructor(private upgradeModule: UpgradeModule, private ngHybridService: NgHybridService) {
}
// noinspection JSUnusedGlobalSymbols
public ngDoBootstrap(): void {
const angularJsModule = angular.module('demo.app', []);
this.ngHybridService.loadNg1Dependencies(angularJsModule);
this.upgradeModule.bootstrap(document.body, ['demo.app'], {strictDi: true});
}
}
See demo source code
git clone https://github.com/domi7777/ng-hybrid-decorators.git
cd ng-hybrid-decorators
npm i
npm run update-demo
npm run demo