I grouped a data array using lodash and can see the output in my console, but my *ngFor loops don't display the data on my html page. Can anyone point out what I'm missing here?
app.component.ts:
export class AppComponent {
name = "Angular";
data = [
{
customer: {
name: "John"
},
transaction_date: "2017-04-18"
},
{
customer: {
name: "Dick"
},
transaction_date: "2017-06-30"
},
{
customer: {
name: "Harry"
},
transaction_date: "2017-04-03"
},
{
customer: {
name: "John"
},
transaction_date: "2017-05-03"
}
];
constructor() {}
ngOnInit() {
// Sort by Month
// Format month
const monthName = item =>
moment(item.transaction_date, "YYYY-MM-DD").format("MMM");
// Establish groupBy array
const byMonth = _.chain(this.data)
.groupBy(monthName)
.mapValues(items => _.map(items, "customer.name"))
.value();
console.log(byMonth);
return byMonth;
console.log(monthName);
// By Day
const dayName = item =>
moment(item.transaction_date, "YYYY-MM-DD").format("DD");
const byDay = _.chain(this.data)
.groupBy(dayName)
.mapValues(items => _.map(items, "customer.name"))
.value();
console.log(byDay);
return byDay;
}
}
app.component.html
<table *ngFor="let month of months">
<tr>
<th>{{month.month}}</th>
</tr>
<tr>
<td>
<table *ngFor="let customer of customers">
<tr>
<td>
{{customer.name}}
</td>
</tr>
</table>
</td>
</tr>
</table>
Desired HTML Format (in a table but the formatting wasn't supported):
- April
- John
- Harry
- May
- Dick
- June
- John
Thanks guys! :)
customersin your component controller, and the correct syntax is*ngFor="let customer of customers". I imagine you have some errors in the console.