Tôi có hai thành phần và một với bộ chọn thuộc tính. Các thành phần con là,Cách truy cập nativeElement của một Component trong Angular4?
import { Component, OnInit, Input, ElementRef } from '@angular/core';
@Component({
selector: '[app-bar-chart]',
templateUrl: './bar-chart.component.html',
styleUrls: ['./bar-chart.component.css']
})
export class BarChartComponent implements OnInit {
@Input() chartContainer: ElementRef;
@Input() title: string;
@Input() chartData: {
title: string,
content: {
label: string,
value: number,
fill?: string
}[]
}[];
constructor() { }
ngOnInit() {
console.log(this.chartContainer);
console.log(this.chartContainer.nativeElement);
}
}
thành phần cha mẹ là,
import { Component, OnInit, Output, ViewChild, ElementRef } from '@angular/core';
@Component({
selector: 'app-dashboard',
template: `<div><div class="graph-image" style="width: 100%; height: 400px;" app-bar-chart [title]="barChartTitle" [chartData]="ChartData" [chartContainer]="chartContainer" #chartContainer></div></div>`,
styleUrls: ['./dashboard.component.css']
})
export class DashboardComponent implements OnInit {
barChartTitle = 'Transaction History';
ChartData = [
{
"title": "May2017",
"content": [
{
"label": "payable",
"value": 10
}
]
},
{
"title": "Jun2017",
"content": [
{
"label": "payable",
"value": 120
}
]
}
];
constructor() { }
ngOnInit() {
}
}
Tôi đi qua các tài liệu tham khảo địa phương từ thành phần cha mẹ để các thành phần con. Khi tôi an ủi phần tử gốc của tham chiếu cục bộ này, nó là 'không xác định'. Làm thế nào tôi có thể truy cập vào phần tử gốc để tôi có thể truy cập chiều rộng và chiều cao kiểu của div thành phần.
Trong trường hợp một phụ huynh chờ đợi childdren ngAddViewInit của nó là chính xác. Tuy nhiên, khi đứa trẻ nhận được phụ huynh ngOnInit nên được ok. Là nó ổn? – kimy82
@ kimy82, tôi hiểu nhầm câu hỏi. Tôi đã cập nhật câu trả lời –