Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 转换对象并按角度6推入数组_Angular_Typescript - Fatal编程技术网

Angular 转换对象并按角度6推入数组

Angular 转换对象并按角度6推入数组,angular,typescript,Angular,Typescript,我有从服务器获取类别的服务和方法getCategory。 方法是: getCategory(): Observable<CategoryModel[]> { return this.http.get<CategoryModel[]> (this.productUrl) .pipe( catchError(this.handleError('getHeroes', [])) ); } 在outputcateg

我有从服务器获取类别的服务和方法getCategory。 方法是:

getCategory(): Observable<CategoryModel[]> {
    return this.http.get<CategoryModel[]> (this.productUrl)
        .pipe(
            catchError(this.handleError('getHeroes', []))
        );
}
在outputcategoryList中是Object,我无法在模板上渲染

因为您得到的是对象而不是数组。您有两个选项,要么将对象转换为数组,要么在html模板中使用KeyValuePipe-

欲了解更多信息,请访问-

因为您得到的是对象而不是数组。您有两个选项,要么将对象转换为数组,要么在html模板中使用KeyValuePipe-


有关更多信息,请访问-

使用firebase,您将获得对象而不是数组。有关更多信息:

因此,一种解决方案是在服务中获取对象时将其转换为数组:

return this.http.get<CategoryModel[]> (this.productUrl)
  .pipe(
    map(products => {
      return Object.values(products);

      // OR

      return Object.keys(products).map((keyName) => {
        return {id: keyName, product: products[keyName]};
      });
    }),
    catchError(this.handleError('getHeroes', []))
  );

使用firebase,您将获得对象而不是数组。有关更多信息:

因此,一种解决方案是在服务中获取对象时将其转换为数组:

return this.http.get<CategoryModel[]> (this.productUrl)
  .pipe(
    map(products => {
      return Object.values(products);

      // OR

      return Object.keys(products).map((keyName) => {
        return {id: keyName, product: products[keyName]};
      });
    }),
    catchError(this.handleError('getHeroes', []))
  );

你也可以用这个

categoryList : CategoryModel[] = [];
然后你可以推送这个用户对象,把它做成一个数组

this.categoryList .push(<<the data you want to push>>);

你也可以用这个

categoryList : CategoryModel[] = [];
然后你可以推送这个用户对象,把它做成一个数组

this.categoryList .push(<<the data you want to push>>);

你在用Firebase吗?是的。我正在使用Firebase.Object.values?使用Firebase,您将获得对象而不是数组。更多信息:你在使用Firebase吗?是的。我正在使用Firebase.Object.values?使用Firebase,您将获得对象而不是数组。有关更多信息:Object.valuesproducts;比KeyValuePipeObject.valuesproducts更干净;超级清洁剂比键值管干净吗