Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
March 26, 2018 06:23 pm

Ionic From Scratch: Working With Ionic Components








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:

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 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 one to develop your own custom components, but that is a topic for another day for advanced usage of Ionic & 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:


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.

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 into 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 make the following changes to the file:

As you can see above, here 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 into our app, lets go ahead and navigate to my-list.html and edit the file as follows:

With the aboe code above added in your app, you should be able to see a list with 3 items.  Now that is 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:

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 docs shows how to use a different approach for populating a list with items:

The magic in the code above is the use of the Angular directive: *ngFor. We won't be diving deeper in to 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 feel the need to 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:

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 changing inside our my-list.ts file:

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 principals apply to the 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. Because, as mentioned right in the beginning, these components are going to be the building blocks of every Ionic application you'll ever build!

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


Original Link:

Share this article:    Share on Facebook
No Article Link

TutsPlus - Code

Tuts+ is a site aimed at web developers and designers offering tutorials and articles on technologies, skills and techniques to improve how you design and build websites.

More About this Source Visit TutsPlus - Code