【问题标题】:How do I read local nested json in Ionic?如何在 Ionic 中读取本地嵌套 json?
【发布时间】:2020-07-08 15:17:38
【问题描述】:

我已经尝试解决这个问题好几个星期了,在整个互联网上寻找,我似乎找不到解决方案。

如何从我的 .json 文件中打印:年份名称和课程名称?我是否必须将career.id 和year.id 绑定在一起才能显示该职业年课程?我怎么能那样做?提前致谢!

.ts 文件

import { Component, OnInit } from '@angular/core';
import { MenuController, NavController } from '@ionic/angular';
import { Router } from '@angular/router';

@Component({
  selector: 'app-carreras',
  templateUrl: './carreras.page.html',
  styleUrls: ['./carreras.page.scss'],
})
export class CarrerasPage implements OnInit {

  carrera: any;

  constructor(
    public menu: MenuController,
    public router: Router,
    public navContrller: NavController,
  ) { }

  ngOnInit() {
    fetch('../../../assets/data/BDs/carreras.json').then(res => res.json()).then(json => {
      this.carrera = json;
    });
  }

}

.html 文件

//This works
    <ion-item class="carreerSelectionButton">
        <ion-label position="floating">Carrera</ion-label>
        <ion-select interface="action-sheet" placeholder="Seleccione su carrera" cancel-text="Cancelar">
            <ion-select-option *ngFor="let c of carrera; index as i">{{c.name}}</ion-select-option>
        </ion-select>
    </ion-item>

/This doesn't work
    <ion-item class="yearSelectionButton">
        <ion-label position="floating">Año</ion-label>
        <ion-select interface="action-sheet" placeholder="Seleccione su carrera" cancel-text="Cancelar">
            <ion-select-option *ngFor="let c of carrera; index as i">{{c.year.name}}</ion-select-option>
        </ion-select>
    </ion-item>

.json 文件

[{
        "id": "LCC",
        "name": "Licenciatura en Ciencias de la Computación",
        "year": [{
                "id": 1,
                "name": "Primer Año",
                "quarter": [{
                        "id": 1,
                        "course": [{
                                "id": "EA",
                                "name": "Elementos de Álgebra"
                            },
                            {
                                "id": "RPA",
                                "name": "Resolución de Problemas y Algoritmos"
                            },
                            {
                                "id": "IC",
                                "name": "Introducción a la Computación"
                            },
                            {
                                "id": "MSI",
                                "name": "Modelados y Sistemas de la Información"
                            }
                        ]
                    },
                    {
                        "id": 2,
                        "course": [{
                                "id": "DDA",
                                "name": "Desarrollo de Algoritmos"
                            },
                            {
                                "id": "EAL",
                                "name": "Elementos de Álgebra Lineal"
                            },
                            {
                                "id": "ETC",
                                "name": "Elementos de Teoría de la Computación"
                            },
                            {
                                "id": "MODATOS",
                                "name": "Modelados de Datos"
                            }
                        ]
                    }
                ]
            },

【问题讨论】:

    标签: javascript html angular typescript ionic-framework


    【解决方案1】:

    如下更改您的代码...

    <div *ngFor="let c of carrera;>   
     <ion-item class="yearSelectionButton">
            <ion-label position="floating">Año</ion-label>
            <ion-select interface="action-sheet" placeholder="Seleccione su carrera" cancel-text="Cancelar">
                <ion-select-option *ngFor="let yName of c.year; index as i">{{yName.name}}</ion-select-option>
            </ion-select>
        </ion-item>
        </div> 
    

    【讨论】:

    • 嗨!谢谢您的回答!我试过了,虽然它似乎添加了 5 个不同的下拉菜单,每个职业一个,如下所示:gyazo.com/4506fb183769378128e9179b8cfad63b。有没有一种方法可以让他们中只有一个显示每个职业对应的年份?
    猜你喜欢
    • 2019-12-10
    • 1970-01-01
    • 1970-01-01
    • 2021-04-11
    • 1970-01-01
    • 2016-01-31
    • 2020-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多