我使用ng2-order-pipe
排序模块为管理(网格)页面实现了排序功能。
例如:
app.module.ts
import { Ng2OrderModule } from 'ng2-order-pipe'; //importing sort module
manage.component.html:从此处调用sort()
<table class="table no-margin">
<thead>
<tr>
<th (click)="sort('name')">Name
<span class="fa fa-sort" *ngIf="key =='name'"></span>
</th>
<th (click)="sort('contact')">Contact No
<span class="fa fa-sort" *ngIf="key =='contact'"></span>
</th>
.... and so on
manage.component.ts:此处声明的sort()
import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import { Http,Response,Headers } from '@angular/http';
import {ActivatedRoute, Router } from '@angular/router';
import 'rxjs/add/operator/toPromise';
@Component({
selector: 'app-manage-enquiry',
templateUrl: './manage.component.html',
styleUrls: ['./manage.component.css']
})
export class ManageEnquiryComponent implements OnInit {
//sorting
key: string = 'name'; //set default
reverse: boolean = false;
//initializing p to one
p: number = 1;
constructor(
//Private todoservice will be injected into the component by Angular Dependency Injector
private http: Http,
private router: Router,
private route: ActivatedRoute
) { }
ngOnInit(): void {...}
sort(key){
this.key = key;
this.reverse = !this.reverse;
}
}
现在我有多个包含网格的组件,并且需要排序功能,但我不想在所有manage.component.ts文件中重复sort()
方法,而是我想把它放进去在app.component.ts这样的父组件中,只需从管理模板中调用它就可以使用所有需要的组件。
但是当我在app.component.ts中添加sort()
时,我收到了以下错误:
ERROR TypeError: _co.sort is not a function
我是棱角分明的新人,对父/子概念及其运作方式不太了解,所以任何帮助都会受到赞赏。