我在玩角7和常春藤,我不能得到一个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"
}我不能在一堆闪电战中复制..。我想问题与常春藤有关。话虽如此,这里是一个堆栈闪电战。
发布于 2019-04-24 09:41:40
在一个新的CLI项目中运行--experimentalIvy标志时,我遇到了同样的问题。
我还创建了一个问题: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-02-14 15:28:41
为了查看输出,您可能需要使用点运算符访问一个属性,否则它将仅仅是[Object Object]。
<ul>
<li *ngFor="let a of assets">
{{a.name}}
</li>
</ul>https://stackoverflow.com/questions/54693678
复制相似问题