简体   繁体   English

具有双向绑定的Angular2组件输入不会触发更改事件

[英]Angular2 component with two-way binding Input not trigger the change event

I have one paginator component to show the page list and detect when the user click to show another page and this component not change the page only must be trigger the event but I cannot watch the change event. 我有一个分页器组件来显示页面列表,并检测用户单击以显示另一页的时间,并且该组件不能更改页面,但这仅是触发事件,而我不能观看更改事件。

The component has an two-way binding to announce the current page changed and work fine because I can show the value in the html after change (please, see 该组件具有双向绑定,可以声明当前页面已更改并可以正常工作,因为更改后我可以在html中显示值(请参见

in the HTML). 在HTML中)。

How I can execute the page change after change the current page in the paginator component? 在分页器组件中更改当前页面后,如何执行页面更改?

I read the following https://stackoverflow.com/questions/36150107/angular-2-change-event-model-changes but not work in my case. 我阅读了以下https://stackoverflow.com/questions/36150107/angular-2-change-event-model-changes,但不适用于我的情况。

HTML (using the paginator): HTML(使用分页器):

<paginator [pageCount]=pageCount [(currentPage)]=currentPage (ngModelChange)="pageChanged($event)"></paginator> <!--pageChanged is not called-->
<p>{{currentPage}}</p> <!--Work Fine showing the new page selected!-->

TS (paginator component): TS(分页器组件):

import { Component, OnInit, OnChanges, SimpleChanges, Input, Output, EventEmitter, ChangeDetectorRef } from '@angular/core';

@Component({
  selector: 'paginator',
  templateUrl: './paginator.component.html',
  styleUrls: ['./paginator.component.css']
})
export class PaginatorComponent implements OnInit, OnChanges {

  pageList : number[] = [1];  

  @Input()  pageCount   : number = 1;
  @Input()  currentPage : number = 1; 
  @Output() currentPageChange = new EventEmitter<number>();

  constructor(private ref:ChangeDetectorRef) {}

  ngOnInit() 
  {
    this.loadPageList();
  }

  ngOnChanges(changes: SimpleChanges)
  {
    this.loadPageList();
  }

  changePage(page : number)
  {
    if(page < 1)
        return;

    if(page > 1 && page > this.pageCount)
      return;

    if(this.currentPage == page)
      return;

    this.currentPage = page;

    this.currentPageChange.emit(this.currentPage);    
  }

  private loadPageList()
  {
      if(this.pageCount < 1)
        this.pageCount = 1;

      let pages = [1];

      for(let p = 2; p <= this.pageCount; p++)
        pages.push(p);

      this.pageList = pages;

      this.ref.detectChanges();          
  }
}

HTML (paginator component): HTML(分页器组件):

<div class="row">
    <div class="col-md-12 text-center">
        <ul class="pagination">
            <li><a (click)="changePage(1)">&laquo;</a></li>
            <li><a (click)="changePage(currentPage - 1)">&#10094;</a></li>
            <li *ngFor="let page of pageList" [class.active]="currentPage==page">
                <a id="changePage{{page}}" (click)="changePage(page)">{{page}}</a>
            </li>
            <li><a (click)="changePage(currentPage + 1)">&#10095;</a></li>
            <li><a (click)="changePage(pageCount)">&raquo;</a></li>
        </ul>
    </div>
</div>

As you are using currentPageChange as your output variable you should use the same to handle the event 当您使用currentPageChange作为输出变量时,应使用相同的变量来处理事件

<paginator [pageCount]=pageCount [currentPage]="currentPage"
         (currentPageChange)="pageChanged($event)"></paginator> 

<p>{{currentPage}}</p> <!--Work Fine showing the new page selected!-->

I know this question is forever old, but I stumbled onto this question yesterday while trying to figure out how to get this working. 我知道这个问题永远存在,但是昨天我在试图弄清楚如何使它工作时偶然发现了这个问题。

I think what the OP is missing in the given example is that you should emit the new value in a setter method for the target property, like this: 我认为在给定示例中缺少的OP是您应该在setter方法中为target属性发出新值,如下所示:

private _currentPage: number = 1;

@Input() get currentPage(): number {
  return this._currentPage;
}

set currentPage(value: number) {
  this._currentPage = value;
  this.currentPageChange.emit(value);
}

@Output() currentPageChange = new EventEmitter<number>();

Simply emitting a new value through an appropriately named EventEmitter is not enough to make the property two-way bound. 仅通过适当命名的EventEmitter发出新值不足以使属性双向绑定。 You need to be emitting the event as a side-effect of setting the value — the way to do that is through a set method. 您需要将事件作为设置值的副作用发出-实现方法是通过set方法。

Here's a minimal StackBlitz example that demonstrates the effect in action: https://stackblitz.com/edit/angular-l9d89t 这是一个最小的StackBlitz示例,演示了实际的效果: https ://stackblitz.com/edit/angular-l9d89t

When the value changes in one place (either the parent or the child component), it seamlessly updates in both places. 当值在一个位置(父组件或子组件)更改时,它将在两个位置无缝更新。 Magic! 魔法!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM