Custom component and ComponentFactoryResolver, how to?


#1

Hey, how should i create a component with the ComponentFactoryResolver? i have a class i’m creating but it looks like it doesn’t get the instance of a service:

// Class to instantiate
@Component({
  selector: 'order',
  templateUrl: 'order.component.html',
  styleUrls: [ 'order.component.scss' ],
  providers: [
    OrderService
  ],
})
export class OrderComponent implements OnInit, OnDestroy {
  @Input() cashier: User;
  @Output() cancel: EventEmitter<number> = new EventEmitter();

  constructor(
    public orderService: OrderService
  ) {
  }

  ngOnInit() {
    this.orderService.cashier = this.cashier;
  }
}
// Class that handles the creation of the instance
@Component({
  selector: 'order-list',
  templateUrl: 'order-list.component.html',
  styleUrls: [ 'order-list.component.scss' ]
})
export class OrderListComponent implements OnInit {
  @ViewChild('body', { read: ViewContainerRef }) body: ViewContainerRef;
  @ViewChild('sidenav') sidenav: MdSidenav;

  @Input() cashier: User;

  public orders: ComponentRef<OrderComponent>[] = [];
  constructor(private resolver: ComponentFactoryResolver) {
  }

  ngOnInit() {
    console.log(this.sidenav, this.body);
    let factory = this.resolver.resolveComponentFactory(OrderComponent);
    debugger
    this.orders.push(this.body.createComponent(factory));
  }
}