2017-02-18 2 views
0

У меня есть div с * ngFor, который загружает значения из json. Мне нужно случайно загрузить значения из разных json-файлов. Здесь div получает значение из 'mydata', в следующий раз мне нужно получить значения от другого json. Как я могу это сделать? я слышал о Math.floor ((Math.random(). Как мне с этим делать? Или какие-либо другие методы?Загружать файлы Json случайным образом в ионном

html file 
     <div class="row" *ngFor="let data of mydata"> 
     <div class="col">{{data.number}}</div> 
     <div class="col">{{data.code}}</div>   
     </div> 



ts file 

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

@Component({ 
selector: 'page-showall', 
templateUrl: 'showall.html' 
}) 
export class ShowallPage { 

constructor(public navCtrl: NavController, public navParams: NavParams) { 

} 

// i am providing data here instead of json for development. 
    datas = [ 
{ 
    number: "1", 
    code: "apple", 

}, 
{ 
    number: "2", 
    code: "orange", 

}, 
{ 
    number: "3", 
    code: "banana", 

},{ 
    number: "50", 
    code: "lemon", 

} 

    ]; 

    mydata = this.datas; 

    // i am using this 'mydata' variable in *ngFor to get values 

} 
+0

с угловым1 я могу? –

+0

им с помощью andgular 2 в ионном 2 – jijishthomas

ответ

0

В своем классе, который вы создали, вы определили, из которого JSON файл, который вы читаете и назначили его DATAS

Создать массив мест, которые вы хотите читать и инициализирует его из конструктора

public locations: Array<string>; 

this.locations = ['firstLocation.json', 'secondLocation.json']; 
let randomJsonFile = this.locations[Math.floor((Math.random() * this.locations.length) + 1)]; 

EDIT:..
(OP добавил более подробный вопрос)

// Outside of the constructor (inside of the class) 

    mydata: Array<Object>; 
    datas0: Array<Object>; 
    datas1: Array<Object>; 
    datas2: Array<Object>; 
    datasCollection: Array<Object>; 

    // Inside of the constructor 

     this.datas0 = [ 
     { number: "1", code: "apple0" }, 
     { number: "2", code: "orange0" }, 
     { number: "3", code: "banana0" }, 
     { number: "50", code: "lemon0" } 
     ]; 

     this.datas1 = [ 
     { number: "1", code: "apple1" }, 
     { number: "2", code: "orange1" }, 
     { number: "3", code: "banana1" }, 
     { number: "50", code: "lemon1" } 
     ]; 

     this.datas2 = [ 
     { number: "1", code: "apple2" }, 
     { number: "2", code: "orange2" }, 
     { number: "3", code: "banana2" }, 
     { number: "50", code: "lemon2" } 
     ]; 

     this.datasCollection.push(this.datas0, this.datas1, this.datas2); 

     this.mydata = this.datasCollection[Math.floor((Math.random() * this.datasCollection.length) + 1)]; 
+0

Я новый ионному, так может у PLS объяснить код у меня есть 5 jsons именно one.json, two.json, three.json, four.json & five.json Мне нужны значения из jsons в следующем коде: mydata = this.datas; Я использую эту 'mydata' в * ngFor = "let data mydata"> – jijishthomas

+0

Обновите свой начальный вопрос более подробным вопросом и добавьте примеры кода вашего класса. –

+0

Я обновил вопрос – jijishthomas

Смежные вопросы