I have many directives built in an Angular 1.x application. Those directives are used by various internal applications at my company. I'd like for those applications to not have to change their code, but instead abstract many of those syntax changes within my directives as I upgrade to Angular 2.x. For example let's say I have the following directive:
<my-directive
my-first-attribute="vm.someProperty"
my-second-attribute="vm.someFunction()"
></my-directive>
Assume I can't change that syntax from the parent component's perspective Those attributes need to be transformed to something like:
[myFirstAttribute]="vm.someProperty"
(click)="vm.someFunction()"
There used to be a compile function where I could make many of these template changes before linking. Now I've got my constructor function where I can pull in an ElementRef
, and DynamicComponentLoader
However, how can I provide vm
to my dynamically loaded component?
I've tried:
this._loader.loadIntoLocation(SubCmp, this._el, 'container')
.then((compRef:ComponentRef) => {
compRef.instance.vm = this.vm;
});
but the component that leverages DynamicComponentLoader
itself doesn't know about the vm
. I really don't want to change the HTML syntax that invokes the component which in turns dynamically loads its own child components. Here is my current plunkr, which is using Angular 2 beta 15.