【发布时间】:2020-10-19 12:18:20
【问题描述】:
点击时不显示图表。我有
我有两个子组件
- 用于显示员工列表的数据组件
- 用于显示员工各自评分的评分组件
我已将“图形代码”放入 Rating 组件的“ngOnInit”函数中,单击按钮位于 Data 组件中,借助事件发射,我试图将布尔值从 Data 传递给 Rating 。 正如您在下面的 rating.component.html 中看到的那样,如果标志值变为 true,则显示图表。但即使 flag 变为 true,图形也不可见。
数据组件
import { DataService } from './../../services/data.service';
import { Component, OnInit, Output, EventEmitter} from '@angular/core';
@Component({
selector: 'app-data',
templateUrl: './data.component.html',
styleUrls: ['./data.component.css'],
})
export class DataComponent implements OnInit {
users: { name: string; city: string }[] = [];
visible = false;
@Output() show = new EventEmitter();
constructor(private dataSerivce: DataService) {}
ngOnInit(): void {
this.users = this.dataSerivce.getEmployees();
}
toggle() {
this.visible = !this.visible;
if (this.visible) {
console.log('enter');
this.show.emit(this.visible);
} else {
this.show.emit(null);
}
}
}
评分组件
import { Rating } from './../../../../models/rating.model';
import { DataService } from 'src/app/services/data.service';
import { Component, OnInit, Input, OnChanges, SimpleChanges} from '@angular/core';
import {Chart} from '../../../../node_modules/chart.js';
@Component({
selector: 'app-rating',
templateUrl: './rating.component.html',
styleUrls: ['./rating.component.css']
})
export class RatingComponent implements OnInit ,OnChanges{
rating: Rating[] = [];
@Input() flag = false;
constructor(private dataService: DataService) { }
ngOnInit() {
console.log('init');
var myChart= new Chart('myChart', {
type: 'bar',
data: {
labels: ['Technical', 'Communication', 'Experience', 'Leadership'],
datasets: [{
label: 'skill scores',
data: [10,15, 16, 22],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)'
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)'
],
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
}
ngOnChanges(changes: SimpleChanges){
console.log('first time');
}
private getRatings($event){
this.dataService.getRating().subscribe(res =>{
this.rating.push(res);
});
}
}
评价 html 代码
<div style="height:40%;width:40%;" class="center" *ngIf ="flag" >
<canvas
class="chart chart-bar"
chart-data="dataChart" id = "myChart"></canvas>
</div>
【问题讨论】:
-
我认为您在问题的 HTML 部分中为 RatingsComponent 发布了相同的代码。你能编辑一下吗?
标签: html angular typescript