代码之家  ›  专栏  ›  技术社区  ›  Eugen-Andrei Coliban HARI PRASAD GANGI

从同一组件中的组件调用方法

  •  0
  • Eugen-Andrei Coliban HARI PRASAD GANGI  · 技术社区  · 6 年前

    所以,我有一个角2分量,这里我还有一个 @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));
        }
    }
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   EvgenyV    6 年前

    只需写下:

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