2016-10-26 17 views
10

मेरे पास एक मास्टरकंपोनेंट है जो हेडर, पाद लेख, साइडबार इत्यादि लोड करता है। शीर्षलेख पर एक ड्रॉपडाउन होता है जिसका उपयोगकर्ता लॉग इन होने के बाद सेट हो जाता है। मैं चाहता हूं कि हेडर स्थिर हो, भले ही मैं अलग-अलग मार्गों पर नेविगेट करता हूं अलग बाल घटक। इसका मतलब है कि चयनित विकल्प नहीं बदला जाना चाहिए और ड्रॉपडाउन का मूल्य सभी बाल घटक में सुलभ होना चाहिए। ड्रॉपडाउन मूल्य का आदान-प्रदान वर्तमान बाल घटक को अद्यतन/पुनः लोड किया जाना चाहिए।मूल घटक को बदलने पर बच्चे घटक को फिर से लोड करें। Angular2

मुझे इस समस्या से कैसे संपर्क करना चाहिए? मैं घटना-श्रोता प्रकार की कार्यक्षमता चाहता हूं। एक बार मास्टरकंपोनेंट चेंज से मॉडल, वर्तमान बाल घटक को फिर से लोड करें। MasterComponent के चर परिवर्तनीय अद्यतन पर ChildComponent अद्यतन को सुनेंगे और कुछ फ़ंक्शन चलाएंगे या फिर कुछ एपीआई कॉल करेंगे और ChildComponent को फिर से लोड करेंगे।

// routes 
const appRoutes: Routes = [ 
    { 
     path: '', 
     redirectTo: 'login', 
     pathMatch: 'full', 
    }, 
    { path: 'login', component: LoginComponent }, 
    { path: 'logout', component: LogoutComponent }, 
    { 
     path: '', 
     component: MasterComponent, 
     canActivate: [AuthGuard], 
     children: [ 
      { path: 'record/create', component: RecordCreateComponent }, // create record for selectedRestaurant in MasterComponent 
      { path: 'record/', component: RecordComponent }, // shows all record of current selectedRestaurant in MasterComponent 
      { path: 'record/:id/update', component:RecordUpdateComponent }, // show form to edit record having id 
      { path: 'record/:id', component: RecordComponent }, // show record details having id 
     ] 
    }, 
    { path: '**', redirectTo: 'login' } 
]; 

// MasterComponent

@Component({ 
    selector: 'master', 
    templateUrl: templateUrl, 
    styleUrls:[styleUrl1] 

}) 
export class MasterComponent implements AfterViewInit, OnInit{ 
    restaurants: Array<Restaurant> = []; 
    user:User; 
    selectedRestaurant: Restaurant; 

    constructor(private router: Router, private storageHelper:StorageHelper){ 
    } 
    ngAfterViewInit() { 
    } 
    ngOnInit(){ 
     this.user = JSON.parse(this.storageHelper.getItem('user')); 
     this.restaurants = this.user.restaurants; 
     this.selectedRestaurant = this.restaurants[0]; 
     this.router.navigate(['/record/' + this.selectedRestaurant.id]); 
    } 
    onRestaurantChange(){ 
     this.router.navigate(['/record/' + this.selectedRestaurant.id]); 
    } 
    createRecord(){ 
    } 
} 

enter image description here

+1

प्रश्न का विषय गुमराह कर रहा है "मूल घटक को पुनः लोड किए बिना बाल घटक बदलें।" आपको बस अपने राउटर को ठीक से सेट करने की आवश्यकता है। ऐसा लगता है कि यह माता-पिता <-> बच्चों की समस्या नहीं है। – jmachnik

+1

@jmachnik मैं बाल घटक से पैरेंट चर का उपयोग करना चाहता हूं और इसके विपरीत। और अभिभावक घटकों का मूल्य सभी मार्गों पर सुसंगत होना चाहिए। – Kaushal

उत्तर

7

उपयोग @Input बच्चे घटकों के लिए अपने डेटा पारित करने के लिए और उसके बाद का उपयोग ngOnChanges (https://angular.io/docs/ts/latest/api/core/index/OnChanges-class.html) अगर उस @Input मक्खी पर परिवर्तित को देखने के लिए।

+0

क्या यह काम करता है जब मैं इनपुट के लिए समान मूल्य भेजता हूं ?? –

+1

@ अभिषेकइकांत निश्चित नहीं है, इसे जांचें। लेकिन अगर यह सेटर्स पर आधारित है, तो मुझे लगता है कि "हाँ", लेकिन यदि आप इसे संदर्भ के माध्यम से मैन्युअल रूप से सेट करते हैं, तो टेम्पलेट में नहीं होना चाहिए। –

+0

यह सेटर्स पर आधारित नहीं है। लेकिन मैंने मान को शून्य में बदल दिया और सही मूल्य पारित किया, यह काम नहीं किया –

1

अपने टेम्पलेट सहित एक घटक को अद्यतन करने के कोणीय पर, वहाँ इस के लिए एक सीधे आगे समाधान है, अपने ChildComponent पर एक @Input संपत्ति होने और अपने @Component डेकोरेटर changeDetection में जोड़ें: इस प्रकार ChangeDetectionStrategy.OnPush:

import { ChangeDetectionStrategy } from '@angular/core'; 
@Component({ 
    selector: 'master', 
    templateUrl: templateUrl, 
    styleUrls:[styleUrl1], 
changeDetection: ChangeDetectionStrategy.OnPush 

}) 
export class ChildComponent{ 
    @Input() data: MyData; 
} 

इनपुट डेटा बदल गया है और घटक को फिर से प्रस्तुत करने के लिए यह जांच के सभी काम करेगा

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