Skip to content

Instantly share code, notes, and snippets.

View msarsha's full-sized avatar
🏠
Working from home

Matan Sar-Shalom msarsha

🏠
Working from home
View GitHub Profile
@msarsha
msarsha / component.ts
Last active December 23, 2017 10:24
angular popover api
@Component({
template:`
<div popover="popoverOne">
// content
</div>
<div popover="popoverTwo">
// content
@msarsha
msarsha / myDir.ts
Last active February 18, 2018 10:20
myDir
@Directive({
selector: '[myDir]'
})
export class MyDirDirective {
constructor(public template: TemplateRef<any>) {
}
}
@msarsha
msarsha / caseClass.ts
Created February 22, 2018 21:12
switch\case angular di
export class CaseOne implements CaseLogicHandler{
key: any = 1;
doLogic() {
// case logic
}
}
export class CaseTwo implements CaseLogicHandler{
key: any = 2;
doLogic() {
export class CaseOne implements CaseLogicHandler{
key: any = 1;
doLogic() {
// case logic
}
}
export class CaseTwo implements CaseLogicHandler{
key: any = 2;
@Injectable()
export class NastyService {
constructor(@Inject(CASE_LOGIC_HANDLER) private caseHandlers: CaseLogicHandler[]) {
}
doNastyOperation(key: any) {
switch (key) {
case 1:
// logic
case 2:
@Injectable()
export class NastyService {
constructor(@Inject(CASE_LOGIC_HANDLER) private caseHandlers: CaseLogicHandler[]) {
}
doNastyOperation(key: any) {
const handler = this.caseHandlers.find(h => h.key === key);
if (!handler) throw new Error(`No case handler provided for key: ${key}`);
handler.doLogic();
}
@Injectable()
export class NastyService {
constructor() {
}
doNastyOperation(key: any) {
switch (key) {
case 1:
// logic
case 2:
@msarsha
msarsha / module.ts
Last active February 23, 2018 18:33
@NgModule({
providers: [
{provide: CASE_LOGIC_HANDLER, useClass: CaseOne, multi: true},
{provide: CASE_LOGIC_HANDLER, useClass: CaseTwo, multi: true},
// ...
]
})
export class AppModule {
}
export interface CaseLogicHandler {
key: any;
doLogic();
}
export const LOGIC_HANDLER = new InjectionToken<CaseLogicHandler>('logic.handlers');