Skip to content

Instantly share code, notes, and snippets.

@cocowalla
Created June 1, 2017 07:30
Show Gist options
  • Save cocowalla/903298fc4d93dd7711b56385f5a3751d to your computer and use it in GitHub Desktop.
Save cocowalla/903298fc4d93dd7711b56385f5a3751d to your computer and use it in GitHub Desktop.
Custom Component attribute that allows template and style inheritance with Angular 4
///<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);
};
}
@cgatian
Copy link

cgatian commented Aug 14, 2017

I dont think this will work with AOT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment