-1

I'm sorry for my little knowledge in Angular. I'm learning it :)

I Would like to display in the template an array field. And it doesn't work in the way I'm doing it. :(

Home.page.TS

appInfo: any[] = [
    {
      title: 'Chapitre 1',
      description: 'Contenu'
    }
  ];

home.page.html

<ion-header>
  <ion-toolbar>
    <ion-title>
      Application de Test
    </ion-title>
  </ion-toolbar>
</ion-header>

<ion-content >
  <h1>{{appInfo.title}}</h1>
  <ion-text>{{appInfo.description}}</ion-text>

</ion-content>

It doesn't show the filed.

JBD
  • 568
  • 8
  • 25
  • You need to loop it with `*ngFor` like `{{data.title}} {{data.description}} ` – Sourav Dutta May 17 '19 at 20:06
  • 2
    Possible duplicate of [How exactly works the Angular \*ngFor in this example?](https://stackoverflow.com/questions/44521677/how-exactly-works-the-angular-ngfor-in-this-example) – dota2pro May 17 '19 at 20:15

2 Answers2

0

Use ngFor to iterate over arrays:

<ion-content *ngFor="let info of appInfo">
  <h1>{{info.title}}</h1>
  <ion-text>{{info.description}}</ion-text>
</ion-content>

If you want to display just the first item:

<ion-content *ngIf="appInfo && appInfo.length > 0">
  <h1>{{appInfo[0].title}}</h1>
  <ion-text>{{appInfo[0].description}}</ion-text>
</ion-content>
pzaenger
  • 11,381
  • 3
  • 45
  • 46
0

Use *ngFor structural directive

<ion-header>
 <ion-toolbar>
   <ion-title>
    Application de Test
  </ion-title>
 </ion-toolbar>
</ion-header>

<ion-content *ngFor="let infor of appInfo">
 <h1>{{infor.title}}</h1>
 <ion-text>{{infor.description}}</ion-text>
</ion-content>

Another long way to get array data is given.

    heroes = ['Windstorm', 'Bombasto', 'Magneta', 'Tornado'];
    myHero1 = this.heroes[0];
    myHero2 = this.heroes[1];
    myHero3 = this.heroes[2];
    myHero4 = this.heroes[3];

.html file

    <p [innerText]="myHero1"></p>
    <p [innerText]="myHero2"></p>
    <p>{{myHero3}}</p>
    <p>{{myHero4}}</p>
Denuka
  • 1,142
  • 3
  • 13
  • 21
  • Your second approach looks interesting too, and useful in some context. Thanks a lot. I will keep this in mind too :) – JBD May 17 '19 at 20:43