3

我正在玩 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

4

2 回答 2

1

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

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

话虽如此,我尝试使用以下命令将 core 和 cli 更新到下一个版本:

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

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

还创建了一个带有分离分支的 Github 存储库,以便于复制,您可以在此处查看:https ://github.com/eliraneliassy/ivy-ngfor-bug

于 2019-04-24T09:41:40.703 回答
0

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

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

堆栈闪电战

于 2019-02-14T15:28:41.887 回答