繁体   English   中英

未定义标识符“长度”。 'null' 不包含这样的成员 ng(0)

[英]Identifier 'length' is not defined. 'null' does not contain such a member ng(0)

错误:- Identifier 'length' is not defined. 'null' does not contain such a member ng(0). Identifier 'length' is not defined. 'null' does not contain such a member ng(0).

下面使用的是 angular 内置管道的键值(即将对象或映射转换为键值对数组。)

.html

<div *ngIf="(widget?.response | keyvalue)?.length"></div>

您最好在组件而不是模板中处理比较登录。 还要考虑keyvalue是一个不纯的管道。 它的调用次数可能比您的实际要求多。 像这样的事情应该这样做

成分:

import { Component, OnInit } from '@angular/core';

export class AppComponent implements OnInit {
  private isWidgetValid = false;

  constructor() { }

  ngOnInit() {
  }

  private setWidgetValidity() {
    if (this.widget) {
      this.isWidgetValid = (Object.entries(this.widget.response)).length ? true : false;
    }
  }
}

模板:

<div *ngIf="isWidgetValid"></div>

如果您只想检查字典是否有值,为什么要使用管道键值?

如果你想遍历你的字典并检查它是否有值,请使用下面的代码。

<div *ngFor="let res of widget?.response| keyvalue;">
  <app-response[res]="res.value"></app-reponse>
</div>

如果您只想检查它是否具有值,请使用 Michael D 的 awnser

暂无
暂无

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

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