显示数据不显示任何内容
2019-05-23
124
我试图在应用程序中的另一个组件内显示来自小部件的一些数据。我创建了 html 小部件文件和 ts 文件,我想显示该组件,但我得到的只是带有文字的卡片,而不是来自公共数据的数字。 控制台没有显示任何问题,除了单击“TypeError:无法读取未定义的属性‘length’”后的长度属性错误,但我不认为这是它不起作用的原因。 我做错了什么?
小部件 html 文件
<mdb-card (click)='toggle()'>
<h4>Revenue</h4>
<p>Actual {{item?.revenue.actual}} $<br>
Change {{item?.revenue.change}} %</p>
</mdb-card>
小部件 ts 文件
export class DsWidgetComponent implements OnInit, OnChanges {
public swiperconfig: SwiperConfigInterface = {
observer: true,
a11y: true,
direction: 'horizontal',
speed: 200,
effect: 'slide',
coverflowEffect: {
slideShadows: false,
depth: 0,
rotate: 30
},
init: true,
loop: false,
freeMode: true,
resistance: true,
resistanceRatio: 0,
spaceBetween: 15,
slidesPerView: 1,
keyboard: true,
mousewheel: true,
nested: true,
};
@Input() public item;
public record = 0;
ngOnChanges() {
}
ngOnInit() {
}
public toggle(): void {
this.record = this.record >= this.item.length - 1 ? 0 : this.record + 1;
}
}
您想要在其中使用来自小部件的信息的组件 html 文件
<div class="container-fluid" *ngIf="data?.length > 0">
<swiper #swiper [config]="swiperconfig">
<ng-container *ngFor="let item of data;">
<div class="swiper-slide ">
<app-ds-widget-component
#card
[data]="{data:item}"
>
</app-ds-widget-component>
</div>
</ng-container>
</swiper>
</div>
它的 ts 文件
export class DashboardComponent {
constructor(
public loginService: LoginService,
private route: ActivatedRoute,
private riotService: RiotService,
private sg: SimpleGlobal,
private translate: TranslateService,
private applicationRef: ApplicationRef,
private router: Router,
private dictionaryService: DictionaryService,
private cookieService: CookieService,
private functionService: FunctionService,
public idle: Idle
) {
}
public data =
[
{'revenue': {'actual': '123', 'change': '25 '},},
{'revenue': {'actual': '23 ', 'change': '23 '},},
{'revenue': {'actual': '43 ', 'change': '12 '},},
];
public swiperconfig: SwiperConfigInterface = {
observer: true,
a11y: true,
direction: 'horizontal',
speed: 200,
effect: 'slide',
coverflowEffect: {
slideShadows: false,
depth: 0,
rotate: 30
},
init: true,
loop: false,
freeMode: true,
resistance: true,
resistanceRatio: 0,
spaceBetween: 15,
slidesPerView: 1,
keyboard: true,
mousewheel: true,
nested: true,
freeModeSticky : true
};
public record = 0;
ngOnInit() {
}
ngOnDestroy() {
}
public toggle(): void {
this.record = this.record >= this.data.length - 1 ? 0 : this.record + 1;
}
}
2个回答
只需将您的 html 从:
[data]="{data:item}"
更改为:
[item]="item"
并将您的 ts 从:
public toggle(): void {
this.record = this.record >= this.data.length - 1 ? 0 : this.record + 1;
}
更改为:
public toggle(): void {
this.record = this.record >= this.item.length - 1 ? 0 : this.record + 1;
}
Baruch Gans
2019-05-23
我有点困惑。你使用
let item
执行 ngFor:
<ng-container *ngFor="let item of data;">
<div class="swiper-slide ">
<app-ds-widget-component
#card
[data]="{data:item}"
>
</app-ds-widget-component>
</div>
</ng-container>
你调用属性
data
并将其提供给你的组件
ds-widget-component
。但随后你使用
{data:item
而不是
item
。在小部件中,你再次将其声明为
item
,但之前将其声明为
data
。也许我错了,但......
应该是:
[item]="item"
widget.ts 内部:
@Input() item;
widget.html 内部
<mdb-card (click)='toggle()'>
<h4>Revenue</h4>
<p>Actual {{item.revenue.actual}} $<br>
Change {{item.revenue.change}} %</p>
</mdb-card>
liqSTAR
2019-05-23