gpt4 book ai didi

angular - ngFor 产生空内容 - Angular 7 IVY

转载 作者:太空狗 更新时间:2023-10-29 18:35:47 25 4
gpt4 key购买 nike

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

和输出:

output

如您所见,我删除了服务返回的 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 中复制...我认为问题与 Ivy 有关。也就是说,here is a 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 存储库以便于复制,您可以在这里查看:https://github.com/eliraneliassy/ivy-ngfor-bug

关于angular - ngFor 产生空内容 - Angular 7 IVY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54693678/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com