ngFor 产生空内容 - Angular 7 IVY

ngFor produces empty content - Angular 7 IVY

我正在玩弄 angular 7 和 ivy,但我无法得到一个 for 循环来吐出任何值。

我的代码:

@Component({
  selector: 'app-asset-list',
  templateUrl: './asset-list.component.html',
  styleUrls: ['./asset-list.component.scss']
})
export class AssetListComponent implements OnInit {

  asset = {name: 'test'};
  assets: Asset[] = [this.asset];
  cols: any[] = [];


  colors: any[] = ['red', 'blue', 'yellow']; // EDIT: added this for debug

  constructor(private assetService: AssetService) {

  }

  ngOnInit() {
    // this.assetService.getAssets().subscribe((value => this.assets = value));
    this.cols = [
      {field: 'name', header: 'Name'}
    ];
  }

}

我的html:

<h3>Asset List</h3>

<!--DEBUG-->
{{ assets | json }}
{{ cols |json}}

<!--Does not show-->
<div *ngIf="assets.length > 0">
  Oh, its greater than 0.
</div>

<!--also does not show-->
<ul>
  <li *ngFor="let a of assets">
    {{a}}
  </li>
</ul>

<!--also does not show-->
<ul>
  <li *ngFor="let c of cols">
    {{c}}
  </li>
</ul>

<!--EDIT: added for debug... also does not show?-->
<ul>
  <li *ngFor="let c of colors">
    {{c}}
  </li>
</ul>

<!--ultimately, this is what I wanted, but the above don't even work-->
<p-table [value]="assets">
  <ng-template pTemplate="header">
    <tr>
      <th *ngFor="let col of cols">
        {{col.header}}
      </th>
    </tr>
  </ng-template>
  <ng-template pTemplate="body" let-asset>
    <tr>
      <td *ngFor="let col of cols">
        {{asset[col.field]}}
      </td>
    </tr>
  </ng-template>
</p-table>

和输出:

如你所见,我把服务返回的Observable给撕掉了,还是不行。我希望在屏幕上看到一些东西,因为调试行显示了一些东西。我错过了什么?

我添加了一个字符串数组 colors 只是为了进行更多的完整性检查...但仍然没有显示。控制台中没有错误。

我的 package.json 依赖项:

"dependencies": {
    "@angular/animations": "7.2.4",
    "@angular/common": "7.2.4",
    "@angular/compiler": "~7.2.4",
    "@angular/core": "7.2.4",
    "@angular/forms": "7.2.4",
    "@angular/platform-browser": "~7.2.0",
    "@angular/platform-browser-dynamic": "~7.2.0",
    "@angular/router": "~7.2.0",
    "core-js": "^2.5.4",
    "primeicons": "^1.0.0",
    "primeng": "^7.0.5",
    "rxjs": "~6.3.3",
    "tslib": "^1.9.0",
    "zone.js": "~0.8.26"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.13.0",
    "@angular/cli": "~7.3.1",
    "@angular/compiler-cli": "7.2.4",
    "@angular/language-service": "7.2.4",
    "@types/node": "~8.9.4",
    "@types/jasmine": "~2.8.8",
    "@types/jasminewd2": "~2.0.3",
    "codelyzer": "~4.5.0",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~3.1.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.1",
    "karma-jasmine": "~1.1.2",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.4.0",
    "ts-node": "~7.0.0",
    "tslint": "~5.11.0",
    "typescript": "~3.2.2"
  }

我无法在 stackblitz 中复制...我认为问题与常春藤有关。也就是说,here is a stackblitz.

您可能需要使用点运算符访问 属性 才能看到输出,否则它将只是 [Object Object]

<ul>
  <li *ngFor="let a of assets">
    {{a.name}}
  </li>
</ul>

STACKBLITZ

当 运行 新 CLI 项目中的 --experimentalIvy 标志时,我 运行 遇到了同样的问题。

我还创建了一个问题: https://github.com/angular/angular/issues/30081

话虽如此, 我尝试使用以下方法将核心和 cli 更新到下一个版本:

ng update @angular/core@next @angular/cli@next

它适用于 V8.0.0-beta.14,所以...我不确定有人会在版本 7 中修复它,而版本 8 即将发布。

还创建了一个 Github 存储库,其中包含单独的 b运行ches 以便于复制,您可以在这里查看:https://github.com/eliraneliassy/ivy-ngfor-bug