【问题标题】:How to emit event from a directive in a component如何从组件中的指令发出事件
【发布时间】:2016-07-19 11:39:53
【问题描述】:

我在我的应用程序中使用 angular2-grid。这是链接 - https://github.com/BTMorton/angular2-grid

所以 NgGrid.d.ts 就像 -

import { ElementRef, Renderer, EventEmitter, DynamicComponentLoader, KeyValueDiffers, OnInit, OnDestroy, DoCheck, ViewContainerRef } from '@angular/core';
import { NgGridConfig, NgGridItemEvent } from "../interfaces/INgGrid";
import { NgGridItem } from "./NgGridItem";
export declare class NgGrid implements OnInit, DoCheck, OnDestroy {
    private _differs;
    private _ngEl;
    private _renderer;
    private _loader;
    private _containerRef;
    onDragStart: EventEmitter<NgGridItem>;
    onDrag: EventEmitter<NgGridItem>;
    onDragStop: EventEmitter<NgGridItem>;
    onResizeStart: EventEmitter<NgGridItem>;
    onResize: EventEmitter<NgGridItem>;
    onResizeStop: EventEmitter<NgGridItem>;
    onItemChange: EventEmitter<Array<NgGridItemEvent>>;
    colWidth: number;
    rowHeight: number;
    minCols: number;
    minRows: number;
    marginTop: number;
    marginRight: number;
    marginBottom: number;
    marginLeft: number;
    isDragging: boolean;
    isResizing: boolean;
    autoStyle: boolean;
    resizeEnable: boolean;
    dragEnable: boolean;
    cascade: string;
    minWidth: number;
    minHeight: number;
    private _items;
    private _draggingItem;
    private _resizingItem;
    private _resizeDirection;
    private _itemGrid;
    private _containerWidth;
    private _containerHeight;
    private _maxCols;
    private _maxRows;
    private _visibleCols;
    private _visibleRows;
    private _setWidth;
    private _setHeight;
    private _posOffset;
    private _adding;
    private _placeholderRef;
    private _fixToGrid;
    private _autoResize;
    private _differ;
    private _destroyed;
    private _maintainRatio;
    private _aspectRatio;
    private _preferNew;
    private _zoomOnDrag;
    private static CONST_DEFAULT_CONFIG;
    private _config;
    config: NgGridConfig;
    constructor(_differs: KeyValueDiffers, _ngEl: ElementRef, _renderer: Renderer, _loader: DynamicComponentLoader, _containerRef: ViewContainerRef);
    ngOnInit(): void;
    ngOnDestroy(): void;
    setConfig(config: NgGridConfig): void;
    getItemPosition(index: number): {
        col: number;
        row: number;
    };
    getItemSize(index: number): {
        x: number;
        y: number;
    };
    ngDoCheck(): boolean;
    setMargins(margins: Array<string>): void;
    enableDrag(): void;
    disableDrag(): void;
    enableResize(): void;
    disableResize(): void;
    addItem(ngItem: NgGridItem): void;
    removeItem(ngItem: NgGridItem): void;
    updateItem(ngItem: NgGridItem): void;
    triggerCascade(): void;
    private _calculateColWidth();
    private _calculateRowHeight();
    private _updateRatio();
    private _onResize(e);
    private _applyChanges(changes);
    private _onMouseDown(e);
    private _resizeStart(e);
    private _dragStart(e);
    private _zoomOut();
    private _resetZoom();
    private _onMouseMove(e);
    private _drag(e);
    private _resize(e);
    private _onMouseUp(e);
    private _dragStop(e);
    private _resizeStop(e);
    private _maxGridSize(w, h);
    private _calculateGridSize(width, height);
    private _calculateGridPosition(left, top);
    private _hasGridCollision(pos, dims);
    private _getCollisions(pos, dims);
    private _fixGridCollisions(pos, dims);
    private _cascadeGrid(pos?, dims?);
    private _fixGridPosition(pos, dims);
    private _isWithinBoundsX(pos, dims);
    private _isWithinBoundsY(pos, dims);
    private _isWithinBounds(pos, dims);
    private _addToGrid(item);
    private _removeFromGrid(item);
    private _updateSize(col?, row?);
    private _filterGrid();
    private _getMaxRow();
    private _getMaxCol();
    private _getMousePosition(e);
    private _getAbsoluteMousePosition(e);
    private _getItemFromPosition(position);
    private _createPlaceholder(item);
}

这是我的组件

import { Component, OnInit ,ViewChild } from '@angular/core';
import {NgGrid, NgGridItem} from 'angular2-grid';

@Component({
  moduleId: module.id,
  selector: 'app-grid',
  templateUrl: 'grid.component.html',
  styleUrls: ['grid.component.css'],
  directives: [NgGrid, NgGridItem]
})
export class GridComponent implements OnInit {
  constructor() { }
  ngOnInit() {

  }


}

我的 HTML 是 -

<div class="grid" [ngGrid]="{'max_cols': 3,'max_rows': 3, 'auto_resize': true}">
  <div class="grid-item" [ngGridItem]="{'sizex': 2, 'sizey': 2}">
    <ng-content></ng-content>
  </div>
</div>

网格工作正常。现在我想在调整网格大小时发出一个事件“onResize”。谁能帮我从控制器发出这样的事件?最好的做法是什么?

【问题讨论】:

    标签: angular angular2-directives


    【解决方案1】:

    你可以像 Günter 建议的那样做,但是从原始网格传递事件:

    export class GridComponent implements OnInit {
        @Output() onResize:EventEmitter = new EventEmitter();
    
        // ...
    }
    

    在 HTML 中将 (onResize)="onResize.emit($event)" 添加到根网格 div,因此您将原始的 onResize 事件传递给您自己的 EventEmitter:

    <div class="grid" [ngGrid]="{'max_cols': 3,'max_rows': 3, 'auto_resize': true}" (onResize)="onResize.emit($event)">
      <div class="grid-item" [ngGridItem]="{'sizex': 2, 'sizey': 2}">
        <ng-content></ng-content>
      </div>
    </div>
    

    并且在您使用 GridComponent 的组件的 HTML 中:

    <app-grid (onResize)="doSomething()"></app-grid>
    

    【讨论】:

    • 效果很好,谢谢我学到了一个很棒的技巧。可以参考一些关于ng2的教程或进阶资料吗?
    【解决方案2】:
    export class GridComponent implements OnInit {
        @Output() onResize:EventEmitter = new EventEmitter();
    
        resize() {
          this.onResize.emit('someValue#);
        }
    }
    

    那么你可以像这样使用它

    <app-grid (onResize)="doSomething()"></app-grid>
    

    【讨论】:

      猜你喜欢
      • 2017-04-01
      • 2016-08-09
      • 1970-01-01
      • 2016-10-24
      • 2017-10-29
      • 2021-09-28
      • 1970-01-01
      • 2018-10-21
      • 1970-01-01
      相关资源
      最近更新 更多