I'm using a component factory to load in different components via a Block
input which states characteristics of the block - internal variables. Then, depending on the variables set, the factory will load in different components.
Here's my code for the factory
import { Directive, Input, AfterContentInit, ComponentFactoryResolver, ViewChild, ViewContainerRef, Component } from '@angular/core';
import { GreenBlock } from './Blocks/GreenBlock';
import { BlueBlock } from './Blocks/BlueBlock';
import { YellowBlock } from './Blocks/YellowBlock';
import { Block } from './Block';
@Component({
selector: 'block-factory',
template: '<div #container></div>',
})
export class BlockFactory implements AfterContentInit {
@Input() block: Block;
@ViewChild('container', {read: ViewContainerRef}) container;
constructor(public resolver: ComponentFactoryResolver)
{
}
ngAfterContentInit() {
var content;
switch(this.block.Type)
{
case "yellow":
content = this.resolver.resolveComponentFactory(YellowBlock);
break;
case "blue":
content = this.resolver.resolveComponentFactory(BlueBlock);
break;
case "green":
content = this.resolver.resolveComponentFactory(GreenBlock);
break;
default:
content = this.resolver.resolveComponentFactory(YellowBlock);
break;
}
let componentRef = this.container.createComponent(content);
componentRef.instance.block = this.block;
}
}
Then, to call this factory via HTML, I use the following notation:
<block-factory (ngModel)="newBlock" [block]="newBlock" style="margin:10px 5px;" ></block-factory>
The newBlock
corresponds to the Block (A class holding some information related to the block), thus depending on what a set the block on screen will look a lot different.
Now, this newBlock
changes depending on what the person picks from a drop-down box. See the following function:
// Set the new block via what is clicked in the dropdown box
setNewBlock(block)
{
this.newBlock = block;
}
A fairly simple function. Now, as expected the newBlock won't update with it's new "look" because Angular can't figure out any of the bindings if they aren't static components. My question is - how can I make Angulars binding function?