我正在运行一个函数来获取类别列表,并为每个类别获取一个id并运行另一个函数来获取该特定类别中我已获得id的“链接”数量。
为此,我有以下代码:
ngOnInit(): void { this.categories = this.categoryService.getCategories(); const example = this.categories.mergeMap((categor) => categor.map((myCateg) => { this.categoryService.countCategoryLinks(myCateg.id) .map(numlinks => Object.assign(myCateg,{numLinks: numlinks})) .subscribe(valeur => console.log(valeur)); return myCateg.id })); example.subscribe(val => console.log("valeur2: "+val)); }getCategories()的位置是:
getCategories(): Observable<any> { return this.category.find({where: {clientId: this.userApi.getCurrentId()}}) };和countCategoryLinks()是:
countCategoryLinks(id: number): Observable<any> { return this.category.countLinks(id) };看来,如下面的屏幕截图所示:
numlinks是一个Object。 当然这是由Object.assign完成的。
有没有办法像categoryName ot clientId而不是Object一样插入“count”?
我的目标是能够在模板中显示所有值 :
<tr *ngFor="let category of categories | async; let id = index"> <td> <label class="mdl-checkbox mdl-js-checkbox mdl-js-ripple-effect mdl-data-table__select" for="row[3]"> <input type="checkbox" id="row[3]" class="mdl-checkbox__input"/> </label> </td> <td class="mdl-data-table__cell--non-numeric"><a [routerLink]="['/category/links']"></a>{{ category.categoryName }} </td> <td class="mdl-data-table__cell--non-numeric">{{category.numLinks}}</td> <td #category.id><i (click)="deleteCategory(id)" class="material-icons">delete</i></td> </tr>I am running a function to get a list of categories and for each category, get an id and run another function to get the number of 'links' in that particular category I have got the id.
For that, I have the following code:
ngOnInit(): void { this.categories = this.categoryService.getCategories(); const example = this.categories.mergeMap((categor) => categor.map((myCateg) => { this.categoryService.countCategoryLinks(myCateg.id) .map(numlinks => Object.assign(myCateg,{numLinks: numlinks})) .subscribe(valeur => console.log(valeur)); return myCateg.id })); example.subscribe(val => console.log("valeur2: "+val)); }where getCategories() is:
getCategories(): Observable<any> { return this.category.find({where: {clientId: this.userApi.getCurrentId()}}) };and countCategoryLinks() is:
countCategoryLinks(id: number): Observable<any> { return this.category.countLinks(id) };It appears, as shown in the screenshot below:
that numlinks is an Object. Of course this is done by Object.assign.
Is there a way to have the "count" inserted like categoryName ot clientId instead of an Object?
My goal is to be able to show all the values in a template:
<tr *ngFor="let category of categories | async; let id = index"> <td> <label class="mdl-checkbox mdl-js-checkbox mdl-js-ripple-effect mdl-data-table__select" for="row[3]"> <input type="checkbox" id="row[3]" class="mdl-checkbox__input"/> </label> </td> <td class="mdl-data-table__cell--non-numeric"><a [routerLink]="['/category/links']"></a>{{ category.categoryName }} </td> <td class="mdl-data-table__cell--non-numeric">{{category.numLinks}}</td> <td #category.id><i (click)="deleteCategory(id)" class="material-icons">delete</i></td> </tr>最满意答案
如果我理解正确,那么你可以简单地这样做:
.map(numlinks => { myCateg.numLinks = numlinks.count; return myCateg; })代替
.map(numlinks => Object.assign(myCateg,{numLinks: numlinks}))因此,您的代码应该是这样的:
{ this.categories = this.categoryService.getCategories(); const example = this.categories .mergeMap((categor) => categor .map((myCateg) => { this.categoryService .countCategoryLinks(myCateg.id) .map(numlinks => { myCateg.numLinks = numlinks.count; return myCateg; }) .subscribe(valeur => console.log(valeur)); return myCateg.id; })); example.subscribe(val => console.log("valeur2: " + val)); }If I understand this correctly, then you can simply do this:
.map(numlinks => { myCateg.numLinks = numlinks.count; return myCateg; })Instead of
.map(numlinks => Object.assign(myCateg,{numLinks: numlinks}))Therefore your code should become something like this:
{ this.categories = this.categoryService.getCategories(); const example = this.categories .mergeMap((categor) => categor .map((myCateg) => { this.categoryService .countCategoryLinks(myCateg.id) .map(numlinks => { myCateg.numLinks = numlinks.count; return myCateg; }) .subscribe(valeur => console.log(valeur)); return myCateg.id; })); example.subscribe(val => console.log("valeur2: " + val)); }更多推荐
发布评论