Sitemap
LearnIonic

This is a tutorial series explaining the implementation of ionic in the most simple manner possible from a pure novice to a near about expert level.

S1C4 : The Ng Family

4 min readApr 20, 2020

--

In my previous post , I talked about JSON , ways it is similar to an array and how they are interchangeably used in real life scenarios as well as the method to understand the type of response being received from the API.

In this post, we will talk about how to display the data received in the HTML part or the skeleton part of the front-end.

In basic JavaScript, we would have used one of the DOM (Document Object Model) element properties such as innerHTML to display the values in any HTML element i.e. it would have be done fully on the scripting side.

In Angular , we need not do that, we can display values using the “ng” family, members which include ngFor , ngModel, ngIf etc.

  • ngFor is used for iterating an array
  • ngModel for taking any form of input
  • ngIf for conditional statements

Let’s understand better with the actual code in action! We will be iterating the regional array and displaying the states as options .

Press enter or click to view image in full size
using ngFor to display options

Some key points about ngFor

  • You need not specify the length of the iteration , it will automatically stop showing results as soon as it has iterated fully
  • You can get the index position of the element (if needed) by specifying another statement
<ion-select><ion-select-option *ngFor="let data of regional"> {{data.loc}} </ion-select-option></ion-select>*ngFor acts in similar fashion as for loop
{{ }} is used to extract and display the value from the variable
Example : {{data.loc}} shows the locationIf you want to show the index position as well
<ion-select>
<ion-select-option *ngFor="let data of regional,let i=index"> {{data.loc}} </ion-select-option></ion-select>
Successfully displaying the states in the option

The next step will be to store the selected option in a variable and trigger a method to display the correct information related to that particular option.

For this purpose, we will be using ngModel.

ngModel binds the value of any input related field (such as select, textarea,input,radio,checkbox) to a variable thereby reducing a lot of boilerplate code to store data entered into an “HTML input element” to a variable.

Get Zack Agarwal’s stories in your inbox

Join Medium for free to get updates from this writer.

An important basic fact about ngModel that should be noted :

[(ngModel)] is for two way binding where () is emitting and [] is for binding

Press enter or click to view image in full size
Attaching the variable “selectedItem”
Declaring the variable in the TS file

An important high level note on the origins of the Ng Family. Read more

So we are done making preparations to store the selected value to the variable. Let us write a method to show the value as soon as an option is selected.

To trigger the method as soon as an option is selected or change we will use the ionChange handler/emitter.

Press enter or click to view image in full size
Attaching ionChange to the ion-select tag

Since the “state” of the ion-select will change as a result of selecting an option, therefore will be attaching the (ionChange) to the ion-select tag.

Since the value selected is being selected in a global variable( more on that later) we need not send the selected value as an argument.

Press enter or click to view image in full size
Alerting or Displaying the selected option
Code in action

Now we need to display the information based on the selected option i.e. showing the respective data about the selected state of India.

Let us now adjust the code accordingly

Press enter or click to view image in full size
Adjusted code for the task
  • The “filter” method works only on arrays , it does the search for us in one line or rather removes unwanted elements which otherwise do not match the query which is in this case the loc key
  • The result returned from the “filter” method is also an array with elements matching the query
  • Since we expect the resultant array to contain one value , we just store the only JSON type value present in the array as the result (global variable)
The search element may have spaces
  • The search element may have spaces since ngModel for select will take the text mentioned between the “ion-select-option” tags if the “value” attribute isn’t specified, thereby, we do a trim() on the searchItem to remove any unwanted trailing spaces from the text.
Press enter or click to view image in full size
spaces were present in the HTML part and value attribute was specififed

Now let’s check our final outcome!

Displaying different data on selection

That’s it for this post . In the next post, I will be working with Ionic UI components (ref: image above) and how to display visually pleasing data without compromising any functionality.

If you haven’t checked my previous post then do so here.

--

--

Zack Agarwal
Zack Agarwal

Written by Zack Agarwal

Ionic/Web Developer | ML | Entrepreneur , Always aroun d for a cup of coffee for a productive discussion regarding development,comics,games ;)

LearnIonic
LearnIonic

Published in LearnIonic

This is a tutorial series explaining the implementation of ionic in the most simple manner possible from a pure novice to a near about expert level.