In this section, you will build an app to demonstrate a custom component with its private variables and template. Observe the following screenshot of the pizza ordering component:
The user will not notice which area is a part of the page as opposed to a self-contained component. Your custom component here is the only area where the list is listening to the Vegetarian checkbox:
Perform the following instructions:
MyComponent
app using the blank
template as shown, and go into the MyComponent
folder:$ ionic start MyComponent blank --v2 $ cd MyComponent
./app/pages/home/home.html
file and replace the content with the following code:<ion-header> <ion-navbar> <ion-title> Pizza App </ion-title> </ion-navbar> </ion-header> <ion-content padding> <ion-card> <ion-card-header> App Homepage </ion-card-header> <ion-card-content> Please start to order your pizza now! </ion-card-content> </ion-card> <my-component></my-component> </ion-content>
This is your root page containing <my-component>
, which will be defined later.
./app/pages/home/home.ts
for editing globally with the following code:import { Component } from '@angular/core'; import { NavController } from 'ionic-angular'; import { MyComponent } from '../../components/foo'; @Component({ selector: 'page-home', templateUrl: 'home.html' }) export class HomePage { constructor(public navCtrl: NavController) { } }
You simply have to declare MyComponent
as a dependency. A component is basically just a directive with a template, assuming you are familiar with the directive concept of Angular 1.
$ mkdir ./src/components
foo.ts
file in the components
directory that you just created, as shown in the following code:import { Component } from '@angular/core'; @Component({ selector: 'my-component', templateUrl: 'foo.html' }) export class MyComponent { public data: any = {myToggle: true}; constructor() { } isClicked(val) { console.log('Vegetarian: ' + val); } }
foo.html
in the ./src/components
folder, as follows:<ion-list> <ion-item> <ion-label>Vegetarian</ion-label> <ion-toggle (click)="isClicked(data.myToggle)" [(ngModel)]="data.myToggle"></ion-toggle> </ion-item> <ion-card *ngIf="data.myToggle"> <ion-card-header> I only eat vegetarian foods </ion-card-header> <ion-list> <button ion-item> Mushroom </button> <button ion-item> Spinach </button> <button ion-item> Red Peppers </button> </ion-list> </ion-card> <ion-card *ngIf="!data.myToggle"> <ion-card-header> I love meat </ion-card-header> <ion-list> <button ion-item> Beef </button> <button ion-item> Chicken </button> <button ion-item> Sausage </button> </ion-list> </ion-card> </ion-list>
./src/app/app.module.ts
, as illustrated, so that you can declare MyComponent
. Observe the following code:import { NgModule } from '@angular/core'; import { IonicApp, IonicModule } from 'ionic-angular'; import { MyApp } from './app.component'; import { HomePage } from '../pages/home/home'; import { MyComponent } from '../components/foo'; @NgModule({ declarations: [ MyApp, HomePage, MyComponent ], imports: [ IonicModule.forRoot(MyApp) ], bootstrap: [IonicApp], entryComponents: [ MyApp, HomePage, MyComponent ], providers: [] }) export class AppModule {}
$ ionic serve
You may wonder why it's necessary to create a component just to toggle a list (of pizza topping options). The answer is that this is just a demonstration of how you can compartmentalize your app using a component. The key things that you have done are as follows:
<my-component>
, which can be used anywhere, including outside your app.To create a component, you need to ensure that you import the decorator
, as shown, from Angular 2 itself (and not from Ionic 2):
import { Component } from '@angular/core'; @Component({ selector: 'my-component', templateUrl: 'foo.html' })
In your component template, everything is local to what is inside the component class. So, you can bind the click event using click
, as shown in the following code:
<ion-item> <ion-label>Vegetarian</ion-label> <ion-toggle (click)="isClicked(data.myToggle)" [(ngModel)]="data.myToggle"></ion-toggle> </ion-item>
Just like Angular 1, you need to use [(ngModel)]
to declare that you want data.myToggle
to be your model. The [(..)]
part is to tell Angular 2 that this is a two-way binding.
There are two lists of pizza toppings. The first one is as follows:
<ion-card *ngIf="data.myToggle"> <ion-card-header> I only eat vegetarian foods </ion-card-header> <ion-list> <button ion-item> Mushroom </button> <button ion-item> Spinach </button> <button ion-item> Red Peppers </button> </ion-list> </ion-card>
The second list of pizza toppings is as shown:
<ion-card *ngIf="!data.myToggle"> <ion-card-header> I love meat </ion-card-header> <ion-list> <button ion-item> Beef </button> <button ion-item> Chicken </button> <button ion-item> Sausage </button> </ion-list> </ion-card>
To toggle the visibility of each list based on the data.myToggle
model, you can use *ngIf
, which is very similar to ng-if
from Angular 1.
3.142.12.170