2016-01-18 19 views
12

यदि शो() कहा जाता है तो मैं डोम को गतिशील घटक जोड़ने की योजना बना रहा हूं। मुझे पता है कि इसे छिपाने के लिए ngIf या [छुपा] के साथ एक समाधान है और इसे निर्देश के रूप में उपयोग करें, लेकिन मैं इस समाधान का प्रशंसक नहीं हूं क्योंकि मैं इसे अपने HTML में घोषित नहीं करना चाहता हूं।कोणीय 2 डीओएम या टेम्पलेट को गतिशील घटक जोड़ें

import {Component} from 'angular2/core'; 
    import {InfoData} from '../../model/InfoData'; 

    @Component({ 
    selector: 'Info', 
    templateUrl: './components/pipes&parts/info.html', 
    styleUrls: ['./components/pipes&parts/info.css'] 
    }) 

    export class Info{ 
    infoData: InfoData; 

    public show(infoData: InfoData) { 
     this.infoData= infoData; 
     document.body.appendChild(elemDiv); <----- Here? 
    } 
    } 

और फिर मैं इसे एक प्रदाता के रूप में घोषित करता हूं ताकि मैं शो() को कॉल कर सकूं।

import {Component} from 'angular2/core'; 
    import {Info} from './components/pipes&parts/Info'; 

    @Component({ 
    selector: 'Admin', 
    templateUrl: './Admin.html', 
    styleUrls: ['./Admin.css'], 
    directives: [Info], 
    providers: [Info] 
    }) 

    export class Admin { 
    constructor(private info: Info) { 
    info.show(); <---- append the Info Element to DOM 
    } 

उत्तर

6

मुझे नहीं लगता कि आपको अन्य घटक के लिए प्रदाता के रूप में Info घटक प्रदान करने की आवश्यकता है। मुझे यकीन नहीं है कि यह भी काम करता है। आप Query लाभ उठा सकते हैं और QueryView घटकों एक दूसरे में इस्तेमाल के संदर्भ के लिए:

@Component({ 
    selector: 'Admin', 
    templateUrl: './Admin.html', 
    styleUrls: ['./Admin.css'], 
    directives: [Info] 
}) 
export class Admin{ 
    constructor(private @Query(Info) info: QueryList<Info>) { 
    info.first().show(); <---- append the Info Element to DOM 
    } 
} 

बजाय Info घटक भीतर तत्व जोड़, आप गतिशील रूप से इस घटक DynamicComponentLoader का उपयोग कर के रूप में गुंटर ने सुझाव दिया जोड़ सकते हैं:

@Component({ 
    selector: 'Info', 
    templateUrl: './components/pipes&parts/info.html', 
    styleUrls: ['./components/pipes&parts/info.css'] 
}) 

export class Info{ 
     infoData: InfoData; 

    public show(infoData: InfoData) { 
    this.infoData= infoData; 
    // No need to add the element dynamically 
    // It's now part of the component template 
    // document.body.appendChild(elemDiv); <----- Here? 
    } 
} 

@Component({ 
    selector: 'Admin', 
    //templateUrl: './Admin.html', 
    // To show where the info element will be added 
    template: ` 
    <div #dynamicChild> 
     <!-- Info component will be added here --> 
    </div> 
    `, 
    styleUrls: ['./Admin.css'], 
    directives: [Info] 
}) 
export class Admin{ 
    constructor(private dcl: DynamicComponentLoader, private eltRef:ElementRef) { 
    this._dcl.loadIntoLocation(Info, this._el, 'dynamicChild') 
     .then(function(el) { 
      // Instance of the newly added component 
     }); 
    } 
} 

उम्मीद है कि यह आपकी मदद करता है, थिएरी

+3

'डायनामिक कॉम्पोनेंट लोडर' अब बहिष्कृत है :( –

+2

@ नोएमीसालुन हाँ, लेकिन इस गुंटर का जवाब आपको रूचि दे सकता है: http: // stackoverflow।कॉम/प्रश्न/36325212/कोणीय-2-गतिशील-टैब-उपयोगकर्ता-क्लिक-चयनित-घटक/36325468 # 36325468 ;-) –

7

अद्यतन

उपयोग ViewContainerRef.createComponent()

एक पूर्ण उदाहरण के लिए देख Angular 2 dynamic tabs with user-click chosen components

मूल

DynamicComponentLoader बहुत पहले

हटा दिया गया था आप इस उद्देश्य के लिए उपयोग कर सकते हैं DynamicComponentLoader, लेकिन यह थोड़ा बोझिल है और बाइंडिंग से संबंधित कुछ मुद्दे हैं।

यह भी देखें:

+0

क्या आप इस प्रश्न को देख सकते हैं, https://stackoverflow.com/questions/47655224/load-ngfor-inside-in sertadjacenthtml। – klaudia

संबंधित मुद्दे