Advertisement
  1. Code
  2. Mobile Development
  3. Ionic Development

Ionic From Scratch: Working With Ionic Components

Scroll to top

What Are Ionic Components? 

Ionic components, for the most part, are what make your hybrid app come to life. Components provide the user interface for your app, and Ionic comes bundled with over 28 components. These will help you create an amazing first impression of your app. 

Of course, you can't use all of these 28 components in a single app. How to decide which ones to use?

Well, luckily there are components that you will find in almost every app. In the previous article I showed you how to navigate to another view using the Ionic Component Button. All we needed to create this button was the following code:

1
<button ion-button>Navigate to Info</button>

Here, we're already using one of the Ionic components available to us. That’s the beauty of Ionic: we don't have to concern ourselves with how the button component is constructed, all we need to know is how to properly use the relevant component. 

When to Use Ionic Components? 

As a beginner, I doubt that there will ever be an app you develop that will not make use of Ionic components. It is also possible for you to develop your own custom components, but that is a topic for another day for advanced usage of Ionic and Angular.

With the above said, let’s have a look at how to use the most commonly used components in Ionic mobile applications:

Slides Component

The slides component normally serves as an intro for apps, and below is an image of its common usage:

Slides used in an intro for an appSlides used in an intro for an appSlides used in an intro for an app

List Component

Lists are one of the components you will also regularly use in your Ionic apps. Take a look at the screenshot below for an example.

Example of a list in an appExample of a list in an appExample of a list in an app

Adding Components to Your Project

Now that we've gathered a bit of info on Ionic components, let's try and put a few of these 'building blocks' together. Let’s go ahead and add some components to our Ionic project.

We will be using the project we created in the previous tutorial, and since home is our app's entry point, we will add slides to the home.html file to add our slides. We will do so by navigating to the home.html file in src/pages/home and making the following changes to the file:

1
<ion-header>
2
  <ion-navbar>
3
    <ion-title>Welcome</ion-title>
4
  </ion-navbar>
5
</ion-header>
6
7
<ion-content>
8
  <!-- Start Ionic Slides Component -->
9
  <ion-slides pager>
10
11
    <ion-slide style="background-color: green">
12
      <h2>Welcome to Hello World</h2>
13
      <p>Do some reading here and swipe left</p>
14
    </ion-slide>
15
  
16
    <ion-slide style="background-color: blue">
17
      <h2>Ionic World</h2>
18
      <p>Some more reading here and swipe left</p>
19
      <p>Swiping right works too :)</p>
20
    </ion-slide>
21
  
22
    <ion-slide style="background-color: red">
23
      <h2>Finish</h2>
24
      <p>You can't swipe all day. See more of my app</p>
25
      <button ion-button (click)="navigateToMyList()">Show me more!</button>
26
    </ion-slide>
27
    
28
  </ion-slides>
29
  <!-- End Ionic Slides Component -->
30
</ion-content>

As you can see above, we've added three slides using the slides component. This is inside <ion-slide>Content here...</ion-slide>. You can generate as many slides as you want, but for the purpose of this example, we've only created three.

We'll use another Ionic component: the list component. In order to do so, let's go ahead and generate a new page titled my-list. You should remember how to generate a new page from the previous tutorial using the following command: ionic generate page my-list.

With our newly created page added to our app, let's go ahead and navigate to my-list.html and edit the file as follows:

1
<ion-header>
2
3
  <ion-navbar>
4
    <ion-title>My List</ion-title>
5
  </ion-navbar>
6
7
</ion-header>
8
9
<ion-content>
10
    <!-- Start Ionic List Component -->
11
    <ion-list>
12
      <ion-item>1</ion-item>
13
      <ion-item>2</ion-item>
14
      <ion-item>3</ion-item>
15
    </ion-list>
16
    <!-- End Ionic List Component -->
17
</ion-content>

With the above code added to your app, you should be able to see a list with three items.  Now that's fine, but I'm sure you'd like to see some smooth scrolling with acceleration and deceleration when you scroll through the list, right? Well, that's easy to achieve—we just need a larger list!

Consider the following code inside the my-list.html file:

1
<ion-header>
2
3
  <ion-navbar>
4
    <ion-title>My List</ion-title>
5
  </ion-navbar>
6
7
</ion-header>
8
9
<ion-content>
10
    <!-- Start Ionic List Component -->
11
    <ion-list>
12
      <ion-item>1</ion-item>
13
      <ion-item>2</ion-item>
14
      <ion-item>3</ion-item>
15
      <ion-item>4</ion-item>
16
      <ion-item>5</ion-item>
17
      <ion-item>6</ion-item>
18
      <ion-item>7</ion-item>
19
      <ion-item>8</ion-item>
20
      <ion-item>9</ion-item>
21
      <ion-item>10</ion-item>
22
      <ion-item>11</ion-item>
23
      <ion-item>12</ion-item>
24
      <ion-item>13</ion-item>
25
      <ion-item>14</ion-item>
26
      <ion-item>15</ion-item>
27
      <ion-item>16</ion-item>
28
      <ion-item>17</ion-item>
29
      <ion-item>18</ion-item>
30
      <ion-item>19</ion-item>
31
      <ion-item>20</ion-item>
32
    </ion-list>
33
    <!-- End Ionic List Component -->
34
</ion-content>

If you update your file with the longer list above, you will see scrolling with acceleration and deceleration. 

Now this is one way of creating a list in our project, but it'll seem pretty annoying if we'll need to create a list with even more items. That would mean writing <ion-item>...content...</ion-item> for each one. Luckily, there is a better way, and even as a beginner, you should try following this same method when working with large sums of data and information. 

The official Ionic documentation shows how to use a different approach for populating a list with items:

1
<ion-header>
2
3
  <ion-navbar>
4
    <ion-title>My List</ion-title>
5
  </ion-navbar>
6
7
</ion-header>
8
9
<ion-content>
10
    <!-- Start Ionic List Component -->
11
    <ion-list>
12
        <button ion-item *ngFor="let item of items" (click)="itemSelected(item)">
13
        {{ item }}
14
        </button>  
15
    </ion-list>
16
    <!-- End Ionic List Component -->
17
</ion-content>

The magic in the code above is the use of the Angular directive: *ngFor. We won't be diving deeper into what this directive is and what it does, but in short, it iterates over a collection of data, allowing us to build data presentation lists and tables in our app. items is a variable that contains our data, and item is filled in with each item in that list. If you want to learn more about this directive, please take a look at the official Angular documentation.

With that knowledge, let's improve our project with the *ngFor directive. Edit the my-list.html file to reflect the following:

1
<ion-header>
2
3
  <ion-navbar>
4
    <ion-title>My List</ion-title>
5
  </ion-navbar>
6
7
</ion-header>
8
9
<ion-content>
10
    <!-- Start Ionic List Component -->
11
    <ion-list>
12
      <ion-item *ngFor="let item of items">
13
        <ion-avatar item-start>
14
          <img src="{{item.image}}">
15
        </ion-avatar>
16
        <h2>{{item.title}}</h2>
17
        <p>{{item.subTitle}}</p>
18
      </ion-item>
19
    </ion-list>
20
    <!-- End Ionic List Component -->
21
</ion-content>

A lot of things are happening here. The <ion-list> contains a series of <ion-avatar> components. The item-start attribute means that the avatar will be aligned to the right-hand side. Each list item also contains a header tag (<h2>) and a paragraph tag (<p>).

So, basically, you can also add additional components inside the list component. Have a look at another great example of how to achieve this in the List In Cards example from the Ionic docs. Again, implementing *ngFor in that example will be of benefit.

Now, back to our code, our item in items contains title, subTitle, and image. Let's go ahead and make the following changes inside our my-list.ts file:

1
export class MyListPage {
2
  items: any;
3
4
  constructor(public navCtrl: NavController, public navParams: NavParams) {
5
    this.items = [
6
      {
7
        title: 'Item 1', 
8
        subTitle: 'Sub title 1', 
9
        image: 'https://placehold.it/50'
10
      },
11
      {
12
        title: 'Item 2', 
13
        subTitle: 'Sub title 2', 
14
        image: 'http://placehold.it/50'
15
      },
16
      {
17
        title: 'Item 3', 
18
        subTitle: 'Sub title 3', 
19
        image: 'http://placehold.it/50'
20
      },
21
      {
22
        title: 'Item 4', 
23
        subTitle: 'Sub title 4', 
24
        image: 'http://placehold.it/50'
25
      },
26
      {
27
        title: 'item 5', 
28
        subTitle: 'Sub title 5', 
29
        image: 'http://placehold.it/50'
30
      },
31
      title: 'item 6', 
32
        subTitle: 'Sub title 6', 
33
        image: 'http://placehold.it/50'
34
      },
35
      title: 'item 7', 
36
        subTitle: 'Sub title 7', 
37
        image: 'http://placehold.it/50'
38
      },
39
      title: 'item 8', 
40
        subTitle: 'Sub title 8', 
41
        image: 'http://placehold.it/50'
42
      },
43
      title: 'item 9', 
44
        subTitle: 'Sub title 9', 
45
        image: 'http://placehold.it/50'
46
      },
47
      title: 'item 10', 
48
        subTitle: 'Sub title 10', 
49
        image: 'http://placehold.it/50'
50
      }]
51
    }

In the example above, we are populating our items inside our constructor file, my-list.ts. These will be displayed inside our page template, the my-list.html file. This data can come from any source: a local database, user input, or an external REST API. But for the sake of this example, we are just hard-coding the data.

Conclusion

Although we didn't cover all of the Ionic components, the same principles apply to the others. I would like to encourage you to play around and test the rest of the components and start getting familiar with using them. As I mentioned right at the beginning, these components are going to be the building blocks of every Ionic application you'll ever build!

In the meantime, check out some of our other posts on Ionic app development.

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.