मुझे आप सब के पहले बता ng-include
Angular1.x
से नहीं है, चलो रों Angular2 द्वारा समर्थित स्पष्ट रूप से $Compile
Angular2
में मौजूद नहीं है। तो, आप CRF-ComponentFactoryResolver
के साथ आगे बढ़ सकते हैं जैसा कि यहां कोणीय संदर्भ के साथ HTML को गतिशील रूप से जोड़ने के लिए दिखाया गया है।
डेमो - (सीएफआर): https://plnkr.co/edit/YdRlULhWQR3L3akAr2eb?p=preview
अपने HTML टुकड़ा कोणीय संदर्भ है, तो आप का उपयोग करना चाहिए CFR-ComponentFactoryResolver
।
sub1.html
में रूप में, आप button
, आप इसे क्लिक करें और उसके क्लिक करें घटना आग चाहते हो सकता है है। यह नीचे के रूप में दिखाया गया है,
आप बहुत कुछ कर सकते CRF
साथ CFR
के साथ प्राप्त किया जा सकता है। यह शायद सबसे आसान उदाहरण है।
@Component({
selector: 'my-app',
template: `
<button (click)="addComponents()">Add HTML (dynamically using CRF)</button>
<h1>Angular2 AppComponent</h1>
<hr>
<div>
<h5>sub1.html goes here</h5>
<div class="container">
<template #subContainer1></template>
</div>
</div>
<hr>
<h5>sub2.html goes here</h5>
<div class="container">
<template #subContainer2></template>
</div>
`,
})
export class App {
name:string;
@ViewChild('subContainer1', {read: ViewContainerRef}) subContainer1: ViewContainerRef;
@ViewChild('subContainer2', {read: ViewContainerRef}) subContainer2: ViewContainerRef;
constructor(
private compFactoryResolver: ComponentFactoryResolver
) {
this.name = 'Angular2'
}
addComponents() {
let compFactory: ComponentFactory;
compFactory = this.compFactoryResolver.resolveComponentFactory(Part1Component);
this.subContainer1.createComponent(compFactory);
compFactory = this.compFactoryResolver.resolveComponentFactory(Part2Component);
this.subContainer2.createComponent(compFactory);
}
}
}
क्या इसमें कोई कोणीय संदर्भ नहीं है? या सिर्फ 'div' और' स्थिर पाठ '? – micronyks
यकीन है कि यह करता है। मेरे पास {{}} और * ngIf, ngFor, ... लेकिन मुख्य टेम्पलेट में अन्य टेम्पलेट्स को शामिल करने में कैसे शामिल हो सकता है? –
क्या इससे मदद मिलती है? http://stackoverflow.com/questions/12863663/complex-nesting-of-partials-and-templates – natel