Я изучаю Angular и сталкиваюсь с проблемой, слишком сложной для моих реальных знаний.
Я пытаюсь создать панель инструментов, используя Material Angular CDK. Каждая карта внутри панели будет отображать другой график. Здесь проблема, компонент, который я передаю компоненту как контент, и он отображается как строка, а не как компонент (скриншот). Структура папок:
- приложение-корень
- Заголовок
- Приборная панель
- GraphType 1
- GraphType 2
- ...
- GraphType n
Ниже вы найдете код для всех задействованных файлов. Дайте мне знать, если чего-то не хватает. Спасибо, кто попытается мне помочь:)
dashboard-rev3.component.html
<div class="grid-container">
<mat-grid-list cols="12" rowHeight="350px">
<mat-grid-tile *ngFor="let card of cards | async" [colspan]="card.cols" [rowspan]="card.rows">
<mat-card class="dashboard-card">
<mat-card-header>
<mat-card-title>
{{card.title}}
<button mat-icon-button class="more-button" [matMenuTriggerFor]="menu" aria-label="Toggle menu">
<mat-icon>more_vert</mat-icon>
</button>
<mat-menu #menu="matMenu" xPosition="before">
<button mat-menu-item>Move</button>
<button mat-menu-item>Edit</button>
<button mat-menu-item>Delete</button>
</mat-menu>
</mat-card-title>
</mat-card-header>
<mat-card-content class="dashboard-card-content">
{{card.content}}
</mat-card-content>
</mat-card>
</mat-grid-tile>
</mat-grid-list>
</div>
----------
dashboard-rev3.component.ts
import { Component } from '@angular/core';
import { map } from 'rxjs/operators';
import { Breakpoints, BreakpointObserver } from '@angular/cdk/layout';
@Component({
selector: 'app-dashboard-rev3',
templateUrl: './dashboard-rev3.component.html',
styleUrls: ['./dashboard-rev3.component.css'],
})
export class DashboardRev3Component {
/** Based on the screen size, switch from standard to one column per row */
cards = this.breakpointObserver.observe(Breakpoints.Handset).pipe(
map(({ matches }) => {
if (matches) {
return [
{ title: 'Active Contracts', cols: 12, rows: 1, content: '<app-graph-bar></app-graph-bar>' },
{ title: 'Building Trends Bar Graph', cols: 12, rows: 1, content: '<app-graph-bar></app-graph-bar>' },
{ title: 'Rate Clock - Currency PPU', cols: 12, rows: 1, content: '<app-graph-bar></app-graph-bar>' },
{ title: 'Building Trends Bar Graph', cols: 12, rows: 1, content: '<app-graph-bar></app-graph-bar>' },
{ title: 'Hammersmith - consumption analysis - YoY Analysis', cols: 12, rows: 1, content: '<app-graph-trend></app-graph-trend>' }
];
}
return [
{ title: 'Active Contracts', cols: 5, rows: 1, content: '<app-graph-bar></app-graph-bar>' },
{ title: 'Building Trends Bar Graph', cols: 7, rows: 1, content: '<app-graph-bar></app-graph-bar>' },
{ title: 'Rate Clock - Currency PPU', cols: 4, rows: 1, content: '<app-graph-bar></app-graph-bar>' },
{ title: 'Building Trends Bar Graph', cols: 8, rows: 1, content: '<app-graph-bar></app-graph-bar>' },
{ title: 'Hammersmith - consumption analysis - YoY Analysis', cols: 12, rows: 1, content: '<app-graph-trend></app-graph-trend>' }
];
})
);
constructor(private breakpointObserver: BreakpointObserver) {}
}
----------
graph-bar.component.ts
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'app-graph-bar',
templateUrl: './graph-bar.component.html',
styleUrls: ['./graph-bar.component.css']
})
export class GraphBarComponent implements OnInit {
dataSource: Object;
constructor() {
this.dataSource = {
"chart": {
"caption": "Countries With Most Oil Reserves [2017-18]",
"subCaption": "In MMbbl = One Million barrels",
"xAxisName": "Country",
"yAxisName": "Reserves (MMbbl)",
"numberSuffix": "K",
"theme": "fusion",
},
// Chart Data
"data": [{
"label": "Venezuela",
"value": "290"
}, {
"label": "Saudi",
"value": "260"
}, {
"label": "Canada",
"value": "180"
}, {
"label": "Iran",
"value": "140"
}, {
"label": "Russia",
"value": "115"
}, {
"label": "UAE",
"value": "100"
}, {
"label": "US",
"value": "30"
}, {
"label": "China",
"value": "30"
}]
};} // end of this.dataSource
ngOnInit() {
}
}