2016-06-07 52 views
0

我正在調用角度2組件中的數據服務以加載ngOnInit()函數上的數據。該組件位於離子選項卡頁面上。 ngOnInit()函數僅在初始化時調用,但不在選項卡的每個導航中調用。我想將數據從每次導航的數據服務重新加載到頁面,以使用最新數據刷新組件。如何刷新每個頁面上的組件更改

如何在每個導航欄上的組件中調用一個功能到標籤頁?

這是我的組件:

@Component({ 
    selector: 'reservation-list', 
    templateUrl: 'build/components/reservation-list.component.html', 
    bindings: [DataService, TimeService] 
}) 
export class ReservationListComponent { 
    public items: any = []; 

    constructor(private dataService: DataService) { } 

    public ngOnInit() { 
    // this I want to call on each tab navigation! 
    this.items = this.dataService.getEvents(); 
    } 
} 

我的標籤基本上都是ionic2-標籤例如:

@Page({ 
    templateUrl: 'build/pages/tabs/tabs.html' 
}) 
export class TabsPage { 
    // this tells the tabs component which Pages 
    // should be each tab's root Page 
    tab1Root: any = Page1; 
    tab2Root: any = Page2; 
    tab3Root: any = Page3; 
} 

和頁面是鹼性離子所在的頁面組件插入:

<reservation-list></reservation-list> 

@Page({ 
    templateUrl: 'build/pages/page1/page1.html', 
    directives: [ReservationListComponent] 
}) 
export class Page1 { 

    constructor() { 
    } 
} 

回答

1

我想你可以添加一個點擊事件處理程序,當你點擊你的標籤並調用該函數。

在你的標籤

<a (click)="getEvents()"></a> 

在您組件

getEvents() { 
    this.items = this.dataService.getEvents(); 
} 
+0

我應該在哪裏放置getEvents呢?在頁面中?該頁面不知道有關該組件的任何信息。 –

+0

你應該在兩個地方。在你的HTML模板和你的組件中。 – Guido

+0

如果我這樣做,是否需要將這些項目標記爲輸入參數? ''? –