Angular2 click事件未触发 [英] Angular2 click event not firing

查看:310
本文介绍了Angular2 click事件未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现自定义分页组件.这是模板.

I'm trying to implement custom pagination component. This is template.

<div *ngIf="totalItems != 0">
    <ul class="pagination">
        <li *ngFor="let page of pages" [ngClass]="{'active': currentPage == page.title}">
            <a (click)="changePage(page)">{{page.title}}</a>
        </li>
    </ul>
    <select>
        <option *ngFor="let size of pageSizes">{{size}}</option>
    </select>
</div>

组件代码:

@Component({
    selector: 'pager',
    templateUrl: 'templates/pager.component.html',
    styleUrls: ['styles/pager.component.css']
})
export class PagerComponent {
    @Input() totalItems: number = 0;
    @Input() lastText: string = "»";
    @Input() firstText: string = "«";
    @Input() nextText: string = "›";
    @Input() prevText: string = "‹";
    public currentPage: number = 1;
    pageSizes: Array<number> = [10, 15, 30];
    public currentSize: number = this.pageSizes[0];

    @Output() pageChanged = new EventEmitter();

    get pages(): Array<IPage> {
        var list = new Array<IPage>();
        let pageCount = Math.ceil(this.totalItems / this.currentSize);
        let start = Math.max(this.currentPage - 1, 1);
        let end = Math.min(this.currentPage + 2, pageCount);
        list.push({ title: this.firstText, number: 1 });
        list.push({ title: this.prevText, number: this.currentPage - 1});
        for (let i = start; i <= end; i++) {
            list.push({ title: String(i), number: i });
        }
        list.push({ title: this.nextText, number: this.currentPage + 1});
        list.push({ title: this.lastText, number: end});
        return list;
    }

    public changePage(page: IPage) {
        this.currentPage = page.number;
        this.pageChanged.emit(null);
    };

    public resetCurrentPage(): void {
        this.currentPage = 1;
    }
}

我正在使用简单的数字数组.然后,我想添加最后/优先"按钮.我编写了包含两个属性标题和页码的界面.现在,点击事件不起作用.我的代码有什么问题?

I was using simple array of numbers. Then I wanted to add Last/Firts buttons. I wrote interface that contains two properties title and page number. Now click event doesn't work. What's wrong with my code?

推荐答案

我已修复它!类似于此问题.每次生成新数组.因此,angular无法将事件绑定到数组项. @GünterZöchbauer,您应该很熟悉.

I've fixed it! It's similar to this question. It generated new array each time. So angular can't bind an event to array item. @Günter Zöchbauer, it should be familiar to you.

这篇关于Angular2 click事件未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆