2
0

added binging between directive and parent component

This commit is contained in:
2018-05-10 21:35:28 +02:00
parent 51e7822b04
commit e4352d9579
5 changed files with 27 additions and 14 deletions

View File

@@ -1,8 +1,6 @@
<canvas appCanvas></canvas> <canvas appCanvas (emitConfig)="updateCanvasConfig($event)"></canvas>
<ul> <ul *ngIf="canvasConfig">
<li>https://github.com/d3/d3-interpolate</li> <li><pre>{{canvasConfig | json}}</pre></li>
<li>https://github.com/d3/d3-timer</li>
<li>https://github.com/d3/d3-scale-chromatic</li>
</ul> </ul>

View File

@@ -9,13 +9,20 @@ canvas {
ul { ul {
display: flex; display: flex;
position: absolute; position: absolute;
top: 0;
right: auto;
bottom: 0; bottom: 0;
left: 0; left: 0;
right: 0;
justify-content: center; justify-content: center;
margin: 0; margin: 0;
padding: 3rem; padding: 3rem;
background: rgba(0,0,0,0.1); background: rgba(0,0,0,0.1);
list-style-type: none; list-style-type: none;
li {
display: flex;
justify-content: center;
align-items: center;
}
} }

View File

@@ -1,4 +1,5 @@
import { Component } from '@angular/core'; import { Component, OnInit, ChangeDetectorRef } from '@angular/core';
import { Config } from './models/config.model';
import * as d3 from 'd3'; import * as d3 from 'd3';
@Component({ @Component({
@@ -7,5 +8,9 @@ import * as d3 from 'd3';
styleUrls: ['./app.component.scss'] styleUrls: ['./app.component.scss']
}) })
export class AppComponent { export class AppComponent {
title = 'app'; public canvasConfig: Config|null;
public updateCanvasConfig(config): void {
this.canvasConfig = config;
}
} }

View File

@@ -1,14 +1,15 @@
import { Directive, ElementRef, Renderer, AfterViewInit, HostListener } from '@angular/core'; import { Directive, ElementRef, Renderer, AfterViewInit, HostListener, Output, EventEmitter, OnInit } from '@angular/core';
import { Config } from './../models/config.model'; import { Config } from './../models/config.model';
@Directive({ @Directive({
selector: '[appCanvas]' selector: '[appCanvas]'
}) })
export class CanvasDirective implements AfterViewInit { export class CanvasDirective implements OnInit {
private canvas: any; private canvas: any;
private context: any; private context: any;
public config: Config; public config: Config;
@Output() emitConfig: EventEmitter<Config>;
@HostListener('window:resize', ['$event']) @HostListener('window:resize', ['$event'])
private onResize(event) { private onResize(event) {
this.updateConfig(); this.updateConfig();
@@ -20,9 +21,10 @@ export class CanvasDirective implements AfterViewInit {
) { ) {
this.canvas = el.nativeElement; this.canvas = el.nativeElement;
this.context = this.canvas.getContext('2d'); this.context = this.canvas.getContext('2d');
this.emitConfig = new EventEmitter();
} }
ngAfterViewInit() { ngOnInit() {
this.updateConfig(); this.updateConfig();
} }
@@ -39,6 +41,7 @@ export class CanvasDirective implements AfterViewInit {
y: this.canvas.clientHeight y: this.canvas.clientHeight
}, },
}; };
console.log(this.config); // Emit Config to parent Component
this.emitConfig.next(this.config);
} }
} }

View File

@@ -1,8 +1,8 @@
import { Point } from './point.model'; import { Point } from './point.model';
export interface Config { export interface Config {
width: Number; width: number;
height: Number; height: number;
start: Point; start: Point;
end: Point; end: Point;
} }