Created
June 1, 2017 07:30
-
-
Save cocowalla/903298fc4d93dd7711b56385f5a3751d to your computer and use it in GitHub Desktop.
Custom Component attribute that allows template and style inheritance with Angular 4
This file contains hidden or 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
///<reference path="../../../../node_modules/reflect-metadata/reflect-metadata.d.ts"/>" | |
import { Component } from "@angular/core"; | |
// Customer Component attribute that allows template and style inheritance (Angular doesn't support this) | |
// Based on https://medium.com/@ttemplier/angular2-decorators-and-class-inheritance-905921dbd1b7 | |
// Updated to work with Angular 4 | |
export function CustomComponent(annotation: any) { | |
return function (target: Function) { | |
var parentTarget = Object.getPrototypeOf(target.prototype).constructor; | |
var parentAnnotations = Reflect.getMetadata("annotations", parentTarget); | |
var parentAnnotation = parentAnnotations[0]; | |
Object.keys(parentAnnotation).forEach(key => { | |
if (parentAnnotation[key] != null) { | |
if (typeof annotation[key] === "function") { | |
annotation[key] = annotation[key].call(this, parentAnnotation[key]); | |
} else if (annotation[key] == null) { // force override in annotation base | |
annotation[key] = parentAnnotation[key]; | |
} | |
} | |
}); | |
var metadata = new Component(annotation); | |
Reflect.defineMetadata("annotations", [ metadata ], target); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I dont think this will work with AOT