【发布时间】:2018-10-24 11:38:56
【问题描述】:
我已经为此绞尽脑汁了一段时间,但终其一生都无法弄清楚哪里出了问题。我有点像 Angular 新手,所以我极有可能做错了一些非常明显的事情。我有以下代码:
bucket.component.html
<div class="container">
<div class="row">
<div class="col s12">
<div id="backButton">
<a class="valign-wrapper" (click)="navigateBack()"><i class="material-icons">arrow_back</i> Back</a>
</div>
<h2 class="center-align" id="title">{{ name }}</h2>
</div>
</div>
<div class="row">
<div class="col s12" *ngFor="let object of objects">
<app-bucket-file [key]="object.Key" [lastModified]="object.LastModified" [size]="object.Size" [owner]="object.Owner.DisplayName"></app-bucket-file>
</div>
</div>
</div>
bucket-file.component.html
<div class="card valign-wrapper">
<div class="card-content white-text">
<span class="card-title">{{ key }}</span>
<p>Last Modified: {{ lastModified | date:'short' }}</p>
<p>Size: {{ conversion(size, 2) }}</p>
<p>Owner: {{ owner }}</p>
</div>
</div>
bucket-file.component.ts
import {Component, Input, OnInit} from '@angular/core';
@Component({
selector: 'app-bucket-file',
templateUrl: './bucket-file.component.html',
styleUrls: ['./bucket-file.component.css']
})
export class BucketFileComponent implements OnInit {
@Input() key: string;
@Input() lastModified: string;
@Input() size: number;
@Input() owner: string;
public conversion = BucketFileComponent.bytesToSize;
constructor() { }
ngOnInit() {
console.log('Key:' + this.key);
}
public static bytesToSize(bytes: number, decimals: number) {
const sizes = ['Bytes', 'KB', 'MB', 'GB,', 'TB'];
if (bytes === 0) {
return '0 Bytes';
}
const k = 1024,
dm = decimals <= 0 ? 0 : decimals || 2,
i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}
}
console.log 在 bucket-file.component.ts ngOnInit() 中工作并返回正确的值,所以我知道输入在某种程度上是有效的。但是,当我尝试在模板 HTML 中显示它时,我什么也得不到。也没有错误。我真的很困惑我做错了什么。任何帮助将不胜感激。
谢谢
【问题讨论】:
-
你能对你的问题进行一次堆栈闪电战吗?
-
背景是白色的吗? :-)
-
它在 NgInit 上工作得很好,因为您正在使用组件对其进行初始化,所以将它加载到 ngOnInit 中的一个变量中......这将起作用
标签: javascript html angular angular6