Hey there!
So there are a few things going on here. I’m going to use the conference app (master branch) as an example of transforming static tabs to dynamic tabs. In the tabs.html
file, the tabs are written like this:
<ion-tabs>
<ion-tab [root]="tab1Root" tabTitle="Schedule" tabIcon="calendar"></ion-tab>
<ion-tab [root]="tab2Root" tabTitle="Speakers" tabIcon="contacts"></ion-tab>
<ion-tab [root]="tab3Root" tabTitle="Map" tabIcon="map"></ion-tab>
<ion-tab [root]="tab4Root" tabTitle="About" tabIcon="information-circle"></ion-tab>
</ion-tabs>
And in the tabs.js
file, the root property for each tab is set like this:
this.tab1Root = SchedulePage;
this.tab2Root = SpeakerListPage;
this.tab3Root = MapPage;
this.tab4Root = AboutPage;
Note that the value being set to each root
property is a @Page
component, and not a string. However, the value being set for tabTitle
and tabIcon
is a string. When a property is wrapped in brackets []
it tells the property to look for an expression, this is known as property binding.
So, we can convert our tabs by using the following template markup:
<ion-tabs>
<ion-tab *ngFor="#tab of tabs" [root]="tab.root" [tabTitle]="tab.title" [tabIcon]="tab.icon"></ion-tab>
</ion-tabs>
We are wrapping each property in brackets because we want it to use the template expression and not the string "tab.title"
, "tab.icon"
, etc. Then, back in our tabs.js
file, we can create the tabs:
this.tabs = [
{ title: "Schedule", root: SchedulePage, icon: "calendar" },
{ title: "Speakers", root: SpeakerListPage, icon: "contacts" },
{ title: "Map", root: MapPage, icon: "map" },
{ title: "About", root: AboutPage, icon: "information-circle" },
];
Angular has a great cheat sheet that is super helpful and it goes over template syntax: https://angular.io/docs/ts/latest/guide/cheatsheet.html
Please let me know if any of the above doesn’t make sense, thanks!