2017-12-27 8 views
0

그래서 두 번째 구성 요소가 있고 여기에 @HostListener도 있습니다. 내가하고 싶은 것은 @HostListener으로이 컴포넌트의 메소드를 호출하는 것이고, @HostListener으로 메소드를 호출하려는 시도로 컴포넌트가 표시됩니다.동일한 구성 요소의 구성 요소에서 메서드 호출

컴포넌트

import { Component, HostListener, Input, OnInit } from '@angular/core'; 
import { Cell } from '../../cell'; 
import { Direction } from '../../direction'; 
import { GridService } from '../../services/grid.service'; 
import { Directive } from '@angular/core/src/metadata/directives'; 

@Component({ 
    selector: 'app-grid', 
    templateUrl: 'grid.component.html', 
    styleUrls: ['grid.component.css'], 
}) 

@Directive({ 
    selector: '[appControl]' 
}) 

export class GridComponentDirective implements OnInit { 

@Input() score: number; 
     best: number; 
     itemArray: Cell[]; 

@HostListener('keyup.ArrowUp') Move(itemArray, UP) { } // That's what I was talking about 

    constructor(private gridService: GridService) { } 

    ngOnInit() { 
     this.itemArray = JSON.parse(localStorage.getItem('Grid')); 
     this.score = 0; 
     if (this.gridService.countEmpty(this.itemArray) === this.itemArray.length) { 
     this.gridService.randomiser(this.itemArray); 
     this.gridService.randomiser(this.itemArray); 
    } 
} 

Move(array: Cell[], direction: Direction) { 
    array = this.gridService.move(array, this.score, direction); 
    localStorage.setItem('Grid', JSON.stringify(array)); 
    } 
} 

답변

2

그냥 쓰기 :

@HostListener('keyup.ArrowUp') MoveUp() {this.Move(itemArray, UP)} 
관련 문제