我想用 *ngFor 显示 Angular2 的集合。
我尝试了类似 *ngFor ="let x in data"{{x._id.category}}
但它不起作用。
我怎样才能显示这个对象或者有没有办法用 Mongoose 来做到这一点?
gquery= [{
$group: {
_id: { category: '$category', specCategory: '$specCategory' },
total:{ $sum:1 }
}
}];
Experiences.aggregate(gquery, function (err, results) {
callback(err, results || {min: 0, max: 0});
});
HTML
<div class="col-sm-3">
<select class="guest categorydd" [(ngModel)]="category_search" [ngModelOptions]="{standalone: true}" (ngModelChange)="changeCategory($event)">
<option value="Alle Kategorien" >All Categories</option>
<option *ngFor="let x of countedCategory " value="{{x._id}}"> {{x._id}} ({{x.total}})</option>
</select>
</div>
组件文件
countedCategory: any[];
this.countedCategory = data[0].priceBoundaries;
最佳答案
据我所知,你应该尝试这样的事情:
@Component({
selector: 'my-app',
template: `
<div>
<h2>Hello {{name}}</h2>
</div>
<div class="col-sm-3">
<select class="guest categorydd" [(ngModel)]="category_search" [ngModelOptions]="{standalone: true}" (ngModelChange)="changeCategory($event)">
<option value="Alle Kategorien" >All Categories</option>
<option
*ngFor="let x of countedCategory"
[value]="x._id.category + '.' + x._id.specCategory">
{{x._id.category}}: {{x._id.specCategory}} ({{x.total}})
</option>
</select>
</div>
`,
})
export class App {
name:string;
category_search;
countedCategory = [
{
_id: { category: 'reisen', specCategory: 'romantik' },
total: 100
},
{
_id: { category: 'reisen', specCategory: 'abenteuer' },
total: 77
},
{
_id: { category: 'reisen', specCategory: 'essen' },
total: 186
}
];
constructor() {
this.name = 'Angular2'
}
private changeCategory(event: any) {
console.log(event);
}
}
您不能使用对象作为它的值
。其结果将是[object object]
!
您需要一个唯一 key 或像上面的示例一样创建一个。
关于javascript - 聚合组多个_id和访问权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41927183/