Hi!
M having a small issue. I have a ts code of loop, but i want that to run in angular as
for(let i = 0; i <= this.Data.length; i++){
this.name = this.Data.name
this.age= this.Data.age
}
console.log(this.name[i] + " Name" + this.age[i] + " Age")
In console it prints great but while printing it prints on single line. I want this to be run in Angular.
<ion-item *ngFor ="let data of Data" >
<div>
{{data.name}}{{data.age}}
</div>
</ion-item>
but in this code it prints all on same line but i want to separate that item.
mvdNL
October 10, 2017, 10:21am
2
<ion-item *ngFor ="let data of Data" >
<div class="user">
{{data.name}}{{data.age}}
</div>
</ion-item>
And in your css
.user {
display: block;
}
Thanks for your reply but this ain’t working.
pwespi
October 10, 2017, 10:25am
4
If you want to separate the age from the name, try:
<ion-item *ngFor="let data of Data">
<h2>{{data.name}}</h2>
<p>Age: {{data.age}}</p>
</ion-item>
WillP44
October 10, 2017, 10:25am
5
Another way is to just put the ion-item inside a list
<ion-list>
<ion-item *ngFor ="let data of Data" >
<div class="user">
{{data.name}}{{data.age}}
</div>
</ion-item>
</ion-list>
name and age will be next to other, here “Data” is an array, its a nested array, in array there is element of name and each name have another array associated with it having age and other details with it. i want to write each index of array elements of name and age
can you display output of your Data variable in Json format,so we can easily understand what you want as output ?
currently my output is:
test,test1 20,22
test and test1 are the names and 20 and 22 are the age. but ts loop console shows:
test 20
test1 22
so i want the out put like console’s.
pwespi
October 10, 2017, 12:25pm
9
Can you display the output of:
console.log(JSON.stringify(this.Data));
[{"personName":"testing","PrescriptionItems":[{"name":["test1","test2"],"medi":[["1"],["2","3"]]}],"MedicenItems":[["1"],["2","3"]]},{"personName":"testing1","PrescriptionItems":[{"name":["test3"],"medi":[["4","5"]]}],"MedicenItems":[["4","5"]]}]
testing {
test1 has 1
test2 has 2,3
}
pwespi
October 10, 2017, 12:45pm
11
Where is this.Data.name
and this.Data.age
?
this is the part of that, in this i have deleted the age and details part
it gives me output as:
test1,test2 1,2,3.
but what i want is that it shows me like
test1 1
test2 2,3
WillP44
October 10, 2017, 1:02pm
15
<ion-list>
<div *ngFor ="let data of Data" >
<ion-item>
{{data.name}}{{data.age}}
</ion-item>
</div>
</ion-list>
Screenshots would be helpful btw
pwespi
October 10, 2017, 1:16pm
16
I suggest you restructure your data from this:
Data = [
{
name: ["test1", "test2"],
medi: [["1"], ["2", "3"]]
}
];
to this:
DataNew = [
{
name: "test1",
medi: ["1"]
},
{
name: "test2",
medi: ["2", "3"]
}
];
and then use
<ion-list>
<ion-item *ngFor="let data of DataNew">
{{data.name}} {{data.medi}}
</ion-item>
</ion-list>
WillP44
October 10, 2017, 1:24pm
17
Much tidier I was going to suggest he do that but didn’t know if it was possible for him to change it.
i will create code based on you json data ,try this
<ion-list>
<div *ngFor ="let data of Data" >
<div *ngFor ="let temp of data.name;let i=index" >
<ion-item>
{{temp[i]}}{{data.medi[i]}}
</ion-item>
</div>
</div>
</ion-list>
<ion-list>
<div *ngFor ="let data of Data" >
<div *ngFor ="let temp of data.name" >
<ion-item>
{{temp}}{{data.medi}}
</ion-item>
</div>
</div>
</ion-list>
worked great after making changes
but now its printing all other ages next to ever name.
ok then what kind of output you have and what you want ?