【问题标题】:Ionic 3 and JSON : trouble to add a name in a gridIonic 3 和 JSON:在网格中添加名称的麻烦
【发布时间】:2018-07-17 01:42:24
【问题描述】:

首先:对不起,我的英语不太好。

我有一个小问题。对于我的考试,我必须创建一个电影院管理应用程序。我才刚刚开始,我已经遇到了问题。

我希望主页显示我必须在名为 home.ts 的文件中注册的电影院名称列表。

但在测试页面并重新启动程序后再次立下遗嘱:我的标题显示但不显示电影院名称列表。

我不知道为什么这不起作用。

我把我当前的代码放在这里,这样你就可以更好地理解我的上下文以及可能是什么问题。

home.html:

 <ion-header>
  <ion-navbar>
    <ion-title>
     Bienvenu sur myCiné
    </ion-title>
  </ion-navbar>
</ion-header>
  <ion-navbar color="primary">
    <button menuToggle ion-button icon-only>
      <ion-icon name="menu"></ion-icon>
    </button>
    <ion-title>myCiné</ion-title>
  </ion-navbar>

  <ion-toolbar color="secondary">
    <ion-title>Mes cinémas favoris</ion-title>
  </ion-toolbar>

<ion-content padding>
  <ion-grid *ngIf="cinemas">
    <ion-row>
      <ion-col col-1 *ngFor="let cinema of cinemas">
        {{cinema.name}}
      </ion-col>
    </ion-row>
  </ion-grid>
</ion-content>

home.ts:

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  cinemas: [
    {
      id: 1,
      name: "Méga CGR La mézière",
      adress: "Zone de Millet",
      cp: "35320",
      ville: "La Mézière",
      nbSalles:"12",
      accesH:"oui",
      proj3D: "oui",
      stand: "oui",
      lesPlus:"Salle ICE"
    },
    {
      id: 2,
      name: "Pathé Atlantis",
      adress: "8 allée de la pérouse",
      cp: "44800",
      ville: "Saint Herblain",
      nbSalles:"12",
      accesH:"oui",
      proj3D: "oui",
      stand: "oui",
      lesPlus:"Salle IMAX"
    },
    {
      id: 3,
      name: "Gaumont Nantes",
      adress: "12 place du commerce",
      cp: "44000",
      ville: "Nantes",
      nbSalles:"14",
      accesH:"oui",
      proj3D: "oui",
      stand: "oui",
      lesPlus:"Salle IMAX"
    },
    {
      id: 4,
      name: "Méga CGR La Rochelle",
      adress: "Avenue Heri Becqurel",
      cp: "17000",
      ville: "La Rochelle",
      nbSalles:"13",
      accesH:"oui",
      proj3D: "oui",
      stand: "oui",
      lesPlus:"Salle ICE"
    }
  ]

  constructor(public navCtrl: NavController) {

  }
}

感谢您的回答,祝您有愉快的一天。

【问题讨论】:

    标签: json ionic3 ion-grid


    【解决方案1】:

    您需要在代码中修复“电影院”的分配:

    import { Component } from '@angular/core';
    import { NavController } from 'ionic-angular';
    
    @Component({
      selector: 'page-home',
      templateUrl: 'home.html'
    })
    export class HomePage {
      // here use assignment rather than comma:
      cinemas = [
        {
          id: 1,
          name: "Méga CGR La mézière",
          adress: "Zone de Millet",
          cp: "35320",
          ville: "La Mézière",
          nbSalles:"12",
          accesH:"oui",
          proj3D: "oui",
          stand: "oui",
          lesPlus:"Salle ICE"
        },
        {
          id: 2,
          name: "Pathé Atlantis",
          adress: "8 allée de la pérouse",
          cp: "44800",
          ville: "Saint Herblain",
          nbSalles:"12",
          accesH:"oui",
          proj3D: "oui",
          stand: "oui",
          lesPlus:"Salle IMAX"
        },
        {
          id: 3,
          name: "Gaumont Nantes",
          adress: "12 place du commerce",
          cp: "44000",
          ville: "Nantes",
          nbSalles:"14",
          accesH:"oui",
          proj3D: "oui",
          stand: "oui",
          lesPlus:"Salle IMAX"
        },
        {
          id: 4,
          name: "Méga CGR La Rochelle",
          adress: "Avenue Heri Becqurel",
          cp: "17000",
          ville: "La Rochelle",
          nbSalles:"13",
          accesH:"oui",
          proj3D: "oui",
          stand: "oui",
          lesPlus:"Salle ICE"
        }
      ]
    
      constructor(public navCtrl: NavController) {
    
      }
    }
    

    此外,为了使它更好,您可以在构造函数之前使用类型声明变量,然后在构造函数中进行赋值。这是一种更标准的方法:

    import { Component } from '@angular/core';
    import { NavController } from 'ionic-angular';
    
    interface Cinema {
        id: number,
        name: string,
        adress: string,
        cp: string,
        ville: string,
        nbSalles: string,
        accesH: string,
        proj3D: string,
        stand: string,
        lesPlus: string
    }
    
    @Component({
        selector: 'page-home',
        templateUrl: 'home.html'
    })
    export class HomePage {
        // here we can declare var and its type:
        cinemas: Array<Cinema>;
    
        constructor(public navCtrl: NavController) {
            // here we can do assignments:
            this.cinemas = [
                {
                    id: 1,
                    name: "Méga CGR La mézière",
                    adress: "Zone de Millet",
                    cp: "35320",
                    ville: "La Mézière",
                    nbSalles: "12",
                    accesH: "oui",
                    proj3D: "oui",
                    stand: "oui",
                    lesPlus: "Salle ICE"
                },
                {
                    id: 2,
                    name: "Pathé Atlantis",
                    adress: "8 allée de la pérouse",
                    cp: "44800",
                    ville: "Saint Herblain",
                    nbSalles: "12",
                    accesH: "oui",
                    proj3D: "oui",
                    stand: "oui",
                    lesPlus: "Salle IMAX"
                },
                {
                    id: 3,
                    name: "Gaumont Nantes",
                    adress: "12 place du commerce",
                    cp: "44000",
                    ville: "Nantes",
                    nbSalles: "14",
                    accesH: "oui",
                    proj3D: "oui",
                    stand: "oui",
                    lesPlus: "Salle IMAX"
                },
                {
                    id: 4,
                    name: "Méga CGR La Rochelle",
                    adress: "Avenue Heri Becqurel",
                    cp: "17000",
                    ville: "La Rochelle",
                    nbSalles: "13",
                    accesH: "oui",
                    proj3D: "oui",
                    stand: "oui",
                    lesPlus: "Salle ICE"
                }
            ]
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-12-04
      • 2015-02-15
      • 2020-08-28
      • 2013-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多