简体   繁体   English

如何在Angular2中将Observable从服务到数组转换为ngFor?

[英]How to transform Observable coming from a service to an Array to ngFor in Angular2?

I have a problem with returns types. 我对退货类型有疑问。 Here is my problem : First my data structure is: {"categoryName":"Google","id":"58591d2b7672d99910497bec","clientId":"585808f6737f6aad1985eab2"},{"categoryName":"Yahoo","id":"58591d4c7672d99910497bef","clientId":"585808f6737f6aad1985eab2"},{"categoryName":"Msn","id":"585d25f6ae4b2ecb056bc514","clientId":"585808f6737f6aad1985eab2"} 这是我的问题:首先,我的数据结构是: {"categoryName":"Google","id":"58591d2b7672d99910497bec","clientId":"585808f6737f6aad1985eab2"},{"categoryName":"Yahoo","id":"58591d4c7672d99910497bef","clientId":"585808f6737f6aad1985eab2"},{"categoryName":"Msn","id":"585d25f6ae4b2ecb056bc514","clientId":"585808f6737f6aad1985eab2"}

I have an HTML file that contains: 我有一个HTML文件,其中包含:

<tr *ngFor="let category of categories; 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.categoryName}}</td>
                         <td #category.id><i (click)="deleteCategory(id)" class="material-icons">delete</i></td>
                    </tr>

The component is as below (I only write here ngOnInit() ): 该组件如下(我只在这里写ngOnInit()):

ngOnInit(): void
    {
        this.categoryService.getCategories().subscribe((data) => {
               this.categories=data;
                console.log("data inside component" + data);
            }
        )
            //console.log(JSON.stringify(this.categories));
    //        .map((data) =>
    //        {
    //            this.categories=data;
    //        })
    }

and the service is: 服务是:

getCategories(){
        return this.category.find({where: {clientId: this.userApi.getCurrentId()}}).map((data) => {

            console.log("type of data: "+ typeof(data));

            console.log ("data:" + JSON.stringify(data));
            //return data;
        }, err => {
            console.log(err);
        });
    };

If I do a console.log from the service, the data is shown. 如果我从服务中执行console.log,则会显示数据。 If I do a console.log from the component, nothing is shown : the message is: 如果我从组件执行console.log,则不会显示任何内容:消息是:

data inside componentundefined

as if the "data" is not passed from the Service to the Component, not passed, or not from the correct type. 就像“数据”没有从服务传递到组件,没有传递或不是从正确的类型传递一样。

Any idea? 任何想法?

Thanks in advance and best regards 预先感谢和最诚挚的问候

uncomment return in map statement. map声明中取消注释返回。 because you do not return anything after do map on data, you got undefined return. 因为在映射数据后您不返回任何内容,所以您得到了undefined返回值。

getCategories() {
   return this.category.find({where: {clientId: this.userApi.getCurrentId()}}).map((data) => {

     console.log("type of data: "+ typeof(data));

     console.log ("data:" + JSON.stringify(data));
     return data; // uncomment this line
   }, err => {
      console.log(err);
   });
}; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM