0

I have this in my html page:

<ion-content padding>
  <ion-list></ion-list>
</ion-content>

I want to add items to this list from my .ts file. How do I do such a thing?

2 Answers 2

4

Rendering HTML on .ts is not a good option, you should add an Array/Observable on .ts and render it using ngFor

<ion-list>
    <ion-item *ngFor="let term of yourArray;">
        <p>{{term}}</p>
    </ion-item>
</ion-list>

.ts Code will be,

yourArray = ['first','second'];
Sign up to request clarification or add additional context in comments.

2 Comments

If I add items to yourArray later during the program, do I have to re-renderfor the items to show up on the list?
no need to do, it uses two way databinding so it will be rendered automatically
0

What do you mean by "adding ion-items to my .ts file"? You are supposed to add ion-items to your ion-list in the .html file.

Try the following:

<ion-content padding>
   <ion-list>
     <ion-item>
        My first item in this list
     </ion-item>

     <ion-item>
       My second item in this list
     </ion-item>
   </ion-list>
</ion-content>

Please refer to the documentation in order to view a full example of ion-items inside ion-lists.

2 Comments

I want to add these items from the ts file
Please check the link of the official documentation I have refered to. There are simple examples for displaying items from variables in your .ts file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.