[英]Dynamically change layout of Angular 2/5 application according to config file
我正在尝试提出一种灵活的布局方式,其功能将像这样。
我当前代码的一个示例:
<div class="row row-mp docDefaultRow">
<div>
<ng-template #firstContainer>
</ng-template>
<!--<app-partner-recipient -> this is the component that gets created in firstContainer
[areItemsSet]="areItemsSet"
(selectedPartnerEmitter)="setPartnerFromChild($event)"
(selectedRecipientEmitter)="selectedRecipient"
[partnerMissing]="partnerMissing">
</app-partner-recipient>-->
<div class="col-md-12 col-lg-6">
<div class="row">
<div class="col-sm-offset-6 col-sm-6">
<div class="form-group">
<label class="control-label" for="date_created">Datum dokumenta</label>
<form #myForm="ngForm" novalidate>
<my-date-picker name="date_created"
id="date_created"
[options]="myDatePickerOptions"
[disabled]="true"
[ngModel]="date_created"
required readonly>
</my-date-picker>
</form>
</div>
</div>
</div>
</div>
</div>
<ng-template #secondContainer>
</ng-template>
<ng-template #thirdContainer>
</ng-template>
并在组件中
@ViewChild('firstContainer', { read: ViewContainerRef }) container1;
@ViewChild('secondContainer', { read: ViewContainerRef }) container2;
public component1Ref: ComponentRef<any>;
public component2Ref: ComponentRef<any>;
public config;
ngOnInit() {
this.createComponents(this.config);
}
createComponents(cfg) {
// How to use config to create components in different places?
this.container1.clear();
const factory1: ComponentFactory<any> = this.resolver.resolveComponentFactory(PartnerRecipientComponent);
this.component1Ref = this.container1.createComponent(factory1);
this.component1Ref.instance.areItemsSet = this.areItemsSet;
this.component1Ref.instance.partnerMissing = this.partnerMissing;
this.component1Ref.instance.selectedPartnerEmitter.subscribe(event => this.setPartnerFromChild(event));
this.component1Ref.instance.selectedPartnerEmitter.subscribe(event => this.selectedRecipient = event);
this.container2.clear();
const factory2: ComponentFactory<any> = this.resolver.resolveComponentFactory(ItemsComponent);
this.component2Ref = this.container2.createComponent(factory2);
this.component2Ref.instance.itemsMissing = this.itemsMissing;
this.component2Ref.instance.isPartnerSet = this.isPartnerSet;
this.component2Ref.instance.selectedPartner = this.selectedPartner;
this.component2Ref.instance.selectedPremiseId = this.selectedPremiseId;
this.component2Ref.instance.ItemsArrEmitter.subscribe(event => this.refreshItems(event));
this.component2Ref.instance.documentSummaryEmitter.subscribe(event => this.refreshDocumentSummary(event));
this.component2Ref.instance.temporaryItemsEmitter.subscribe(event => this.temporaryItems = event);
this.component2Ref.instance.arePricesUpdated.subscribe(event => this.pricesUpdated(event));
}
我基本上不知道如何在我想要的任何ng模板中创建组件,根据配置文件,我会收到。
我认为整个过程有点蛮力的。 我所做的是重组了子组件,并使它们通过我订阅的包含子组件的服务通过服务发出价值。
然后,我检索一个配置文件,并将其设置为一个behaviorSubject,可供订阅。
我订阅cfg,并像这样处理发出的值:
this.config.forEach(component => {
if (component === 'partnerComponent') {cfg.push(PartnerRecipientComponent);
} else if (component === 'itemsComponent') {cfg.push(ItemsComponent);
} else if (component === 'summaryComponent') {cfg.push(OrderSummaryComponent);
} else if (component === 'dateComponent') {cfg.push(OrderDateComponent);
} else if (component === 'numberComponent') {cfg.push(OrderNumberComponent);
} else if (component === 'methodComponent') {cfg.push(SendingMethodComponent);
} else if (component === 'remarkComponent') {cfg.push(OrderRemarkComponent);
} else if (component === 'timepickerComponent') {cfg.push(TimepickerComponent);
} else {cfg.push(undefined); }
});
this.createComponents(cfg);
createComponents(cfg) {
const containers = [this.container1, this.container2, this.container3, this.container4, this.container5, this.container6, this.container7, this.container8, this.container9, this.container10];
for (let i = 0; i < cfg.length; i++) {
if (cfg[i] === undefined) {continue; }
containers[i].clear();
const factory: ComponentFactory<any> = this.resolver.resolveComponentFactory(cfg[i]);
this.componentsArray.push(containers[i].createComponent(factory));
}
}
并创建组件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.