代码之家  ›  专栏  ›  技术社区  ›  Nicke Manarin

如何封装具有事件的自定义角度组件?

  •  0
  • Nicke Manarin  · 技术社区  · 3 年前

    我用的是 pagination component 来自ng bootstrap(我定制了它),我希望在其他地方重用该组件,而无需重写代码。

    ngb-pagination 组件具有双向绑定属性 page ,我需要正确地将该属性公开给父组件。

    我知道问题出在下面的组件上,因为如果我使用标准组件,而不是在组件内部,它会正常工作。

    import { Component, Input, Output, OnInit, EventEmitter } from '@angular/core';
    
    import { faChevronLeft, faChevronRight, faAngleDoubleLeft, faAngleDoubleRight } from '@fortawesome/free-solid-svg-icons';
    
    @Component({
      selector: 'app-pagination',
      template: `
        <div class="d-flex justify-content-between p-2">
          <ngb-pagination [collectionSize]="collectionSize" [page]="page" (pageChange)="pageChange" [pageSize]="pageSize" [boundaryLinks]="true">
            <ng-template ngbPaginationFirst>
              <fa-icon class="fas" [icon]="faAngleDoubleLeft" aria-hidden="true"></fa-icon>
            </ng-template>
    
            <ng-template ngbPaginationLast>
              <fa-icon class="fas" [icon]="faAngleDoubleRight" aria-hidden="true"></fa-icon>
            </ng-template>
    
            <ng-template ngbPaginationPages let-page let-pages="pages">
              <li class="ngb-custom-pages-item" *ngIf="pages.length > 0">
                  <div class="form-group d-flex flex-nowrap px-2">
                    <label id="paginationInputLabel" for="paginationInput" class="col-form-label mr-2 ml-1">Page</label>
    
                    <input #i type="text" inputmode="numeric" pattern="[0-9]*" class="form-control custom-pages-input"
                        id="paginationInput" [value]="page" (keyup.enter)="selectPage(i.value)" (blur)="selectPage(i.value)"
                        (input)="formatInput($any($event).target)" aria-labelledby="paginationInputLabel paginationDescription"
                        style="width: 2.5rem"/>
    
                    <span id="paginationDescription" class="col-form-label text-nowrap px-2">of {{pages.length}}</span>
                  </div>
              </li>
            </ng-template>
    
            <ng-template ngbPaginationPrevious>
              <fa-icon class="fas" [icon]="faChevronLeft" aria-hidden="true"></fa-icon>
            </ng-template>
    
            <ng-template ngbPaginationNext>
              <fa-icon class="fas" [icon]="faChevronRight" aria-hidden="true"></fa-icon>
            </ng-template>
          </ngb-pagination>
        </div>
      `,
      styleUrls: ['./pagination.component.scss']
    })
    export class PaginationComponent implements OnInit {
      faChevronLeft = faChevronLeft;
      faChevronRight = faChevronRight;
      faAngleDoubleLeft = faAngleDoubleLeft;
      faAngleDoubleRight = faAngleDoubleRight;
    
      @Input() collectionSize: number = 20;
      @Input() page: number = 1;
      @Output() pageChange = new EventEmitter<number>();
      @Input() pageSize: number = 10;
    
      constructor() { }
    
      ngOnInit(): void {
      }
    
      selectPage(page: string) {
        this.page = parseInt(page, 10) || 1;
      }
    
      formatInput(input: HTMLInputElement) {
        input.value = input.value.replace('/[^0-9]/g', '');
      }
    }
    

    我简单地使用组件如下(我遵循 complete table example ):

    <app-pagination [collectionSize]="(total$ | async)!" [(page)]="service.page" [pageSize]="service.pageSize">
    </app-pagination>
    

    那么,如何正确地公开属性和事件呢?

    0 回复  |  直到 3 年前
        1
  •  1
  •   IAfanasov Shreyas Dhanakshirur    3 年前

    您需要从组件发出事件:

    <ngb-pagination [collectionSize]="collectionSize" [page]="page" (pageChange)="pageChange.emit($event)" [pageSize]="pageSize" [boundaryLinks]="true">
    

    ...

      selectPage(page: string) {
        this.page = parseInt(page, 10) || 1;
        this.pageChange.emit(this.page);
      }