Angular8-无法遍历对象

时间:2019-09-02 19:38:46

标签: javascript angular observable angular8 behaviorsubject

我正在使用Angular8,下面有以下代码。

问题:

  1. 为什么组件中的Observable只具有一项(应该有四项)。
  2. 在html中,为什么不能迭代该对象?我认为这是一个数组。

服务:

private listProductsJSON: any;
private listProducts$: BehaviorSubject<Product[]>;

public getItems(url: string): Observable<Product[]> {
  this.listProducts$ = <BehaviorSubject<Product[]>> new BehaviorSubject({});
  this.listProductsJSON = require('/Users/richardmarais/Development/examples/angular-menu-routes/menu-routes-app/src/app/model/products.json');  // gets some hard coded json with 4 rows
  Object.keys(this.listProductsJSON.products).map(key => {
    //console.log(this.listProductsJSON.products[key]); // logs 4 rows
    this.listProducts$.next(this.listProductsJSON.products[key]);
  });

  return this.listProducts$.asObservable();
}

组件:

private productsData: Product[];

ngOnInit() {
    this._productService.getItems(this.dataService.serviceData.URL).subscribe((products: Product[]) => {
        console.log(products);  // only logs 1 row        
        this.productsData = products;
    });
}

html:

    <ul>
      <div *ngFor="let product of productsData">
        <li>{{ product }}</li>
      </div>
    </ul>

控制台日志:(应该是四个项目,但只能是一个)

{status: "validated", name: "lions", description: "", id: "yXfhYcDpzYmyWGYptNme", order: 4}

错误:

ProductsComponent.html:5 ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'lions'. NgFor only supports binding to Iterables such as Arrays.
    at NgForOf.ngDoCheck (common.js:4841)
    at checkAndUpdateDirectiveInline (core.js:31912)
    at checkAndUpdateNodeInline (core.js:44366)
    at checkAndUpdateNode (core.js:44305)
    at debugCheckAndUpdateNode (core.js:45327)
    at debugCheckDirectivesFn (core.js:45270)
    at Object.eval [as updateDirectives] (ProductsComponent.html:5)
    at Object.debugUpdateDirectives [as updateDirectives] (core.js:45258)
    at checkAndUpdateView (core.js:44270)
    at callViewAction (core.js:44636)
View_ProductsComponent_1 @ ProductsComponent.html:5
logError @ core.js:45545
handleError @ core.js:6066
(anonymous) @ core.js:41057
invoke @ zone-evergreen.js:359
run @ zone-evergreen.js:124
runOutsideAngular @ core.js:39571
tick @ core.js:41054
(anonymous) @ core.js:40892
invoke @ zone-evergreen.js:359
onInvoke @ core.js:39698
invoke @ zone-evergreen.js:358
run @ zone-evergreen.js:124
run @ core.js:39510
next @ core.js:40889
schedulerFn @ core.js:35335
__tryOrUnsub @ Subscriber.js:185
next @ Subscriber.js:124
_next @ Subscriber.js:72
next @ Subscriber.js:49
next @ Subject.js:39
emit @ core.js:35297
checkStable @ core.js:39641
onHasTask @ core.js:39718
hasTask @ zone-evergreen.js:411
_updateTaskCount @ zone-evergreen.js:431
_updateTaskCount @ zone-evergreen.js:264
runTask @ zone-evergreen.js:185
drainMicroTaskQueue @ zone-evergreen.js:559
invokeTask @ zone-evergreen.js:469
invokeTask @ zone-evergreen.js:1603
globalZoneAwareCallback @ zone-evergreen.js:1629
Show 2 more frames
ProductsComponent.html:5 ERROR CONTEXT DebugContext_ {view: {…}, nodeIndex: 5, nodeDef: {…}, elDef: {…}, elView: {…}}

product.ts

export class Product {
    status:      string;
    name:        string;
    description: string;
    id:          string;
    order:       number;
 }

products.json

{
    "products": {
        "drivers": {
            "status": "validated",
            "name": "drivers",
            "description": "",
            "id": "5rI6n3O2T2z3uKLqbEGj",
            "order": 1
        },
        "zombies": {
            "status": "validated",
            "name": "zombies",
            "description": "active",
            "id": "e6T8tOrbUQH1qyo2ECfj",
            "order": 2
        },
        "passengers": {
            "status": "validated",
            "name": "passengers",
            "description": "",
            "id": "tP4SkWIYRubHN6mCZRA5",
            "order": 3
        },
        "lions": {
            "status": "validated",
            "name": "lions",
            "description": "",
            "id": "yXfhYcDpzYmyWGYptNme",
            "order": 4
        }
    }
}

3 个答案:

答案 0 :(得分:1)

您应该使用 keyvalue 管道迭代对象。

例如

HTML

 <ul>
  <div *ngFor="let product of productsData | keyvalue">
    <li>{{ product.value.status }}</li>
  </div>
</ul>

答案 1 :(得分:0)

这是您的JSON:

{
    "products": {
        "drivers": {
        }
        ... (3 more objects)
     }
}

如您在此处看到的,您已经制作了一个值为“ products”的对象。如果要将其放入变量中,则可以通过object.products进行访问。 “产品”对象具有4个属性,drivers是其中之一(object.products.drivers)。您期望一个array有4个对象。

当您尝试在此处将json转换为打字稿对象时:(products: Product[]),它静默地(不确定是否为此进行记录)找不到数组,而是将“ products”对象放入数组中。

要解决此问题,您需要修复JSON->

{
    "products": [ <- array of objects, not an object with properties
        "drivers": {
        }
     ]
}

为什么要这样做?

this.productsData = Object.values(products).sort((a,b)=>b.order-a.order);

Object.values(),从您为其提供的“产品”对象中获取所有值,并将其放入一个可迭代的数组中。

答案 2 :(得分:0)

products不是数组,JSON是嵌套对象

另外,您应该使用接口而不是export class Product

   export interface Drivers {
        status: string;
        name: string;
        description: string;
        id: string;
        order: number;
    }

    export interface Zombies {
        status: string;
        name: string;
        description: string;
        id: string;
        order: number;
    }

    export interface Passengers {
        status: string;
        name: string;
        description: string;
        id: string;
        order: number;
    }

    export interface Lions {
        status: string;
        name: string;
        description: string;
        id: string;
        order: number;
    }

    export interface Products {
        drivers: Drivers;
        zombies: Zombies;
        passengers: Passengers;
        lions: Lions;
    }

    export interface ProductRootObject {
        products: Products;
    }