I am writing my web apps with AngularJs and TypeScript. That works great, but I've a conceptual problem. In most cases I use AngularJs Services to encapsulate some functionality like a typed abstraction for localstorage.
But I am not sure how to handle some simple helper functions, create a simple helper service with AngularJs or simple class with TypeScript whats the best way in a AngularJs project.
I would also reuse this helper functions in my other AngularJs Projects
Example implemented as service at the moment:
module App.Views.Shared {
export interface IPasteSrv {
getPastedText($pasteEvent): string;
}
export class PasteSrv implements IPasteSrv {
static $inject = [];
constructor() { }
public getPastedText($pasteEvent): string {
var pastedText = "";
if (window.clipboardData) { //IE
pastedText = window.clipboardData.getData('Text');
} else if ($pasteEvent.originalEvent.clipboardData) {
try {
pastedText = $pasteEvent.originalEvent.clipboardData.getData('text/plain');
} catch (ex) {
pastedText = undefined;
}
}
if (pastedText) {
//verhindern das der Text im Model eingefügt wird.
$pasteEvent.preventDefault();
}
return pastedText;
}
//#region Angular Module Definition
private static _module: ng.IModule;
public static get module(): ng.IModule {
if (this._module) {
return this._module;
}
this._module = angular.module('pasteSrv', []);
this._module.service('pasteSrv', PasteSrv);
return this._module;
}
//#endregion
}
}