【发布时间】:2018-12-05 04:06:40
【问题描述】:
我正在构建一个组件。这是一个带有分页的简单列表,可以从 Web 服务中获取数据。我正在使用引导程序列出值和 ngx-bootstrap 分页组件。
问题是 currentPage 属性不能正常工作。 该值已附加到变量,但组件显示的信息错误。
在组件内部,变量具有@Input 装饰器
@Input() listFields?: string[]; //list of displayable fields in the list
@Input() resourceService: ResourceService<T>; //webservice to fetch data
@Input() pageSize ?: number; //number of items to receive from the webservice
@Input() currentPage ?: number = 1; //current page
@Input() modelParams?: HttpParams; //other parameters to send to the webservice
上面描述的变量是从ngx-bootstrap发送到分页组件的。
<pagination [(itemsPerPage)]="pageSize" [(ngModel)]="currentPage" [(totalItems)]="page.totalCount"
(pageChanged)="pageChanged($event)"></pagination>
我正在开发的组件接收来自@inputs 的值,如下所述。
<general-resource-list
[resourceService]="workshopService"
[listFields]="listFieldsWorkshop"
[currentPage]="currentPage"
[pageSize]="pageSize"
[modelParams]="pageParams">
</general-resource-list>
@Component({
selector: 'app-workshops-list',
templateUrl: './workshops-list.component.html',
styleUrls: ['./workshops-list.component.scss']
})
export class WorkshopsListComponent{
public listFieldsWorkshop = ['id','name','version'];
public title = "Workshop List";
public currentPage:number;
public pageSize:number;
public pageParams:HttpParams;
constructor(
public workshopService: WorkshopService,
public workShopRoute: ActivatedRoute,
public workshopRouter: Router) {
this.workShopRoute.queryParams.subscribe(params => {
this.pageParams = new HttpParams();
for(let key in params){
if(!["page","pageSize"].includes(key)){
this.pageParams = this.pageParams.append(key,params[key]);
}
}
this.currentPage = params['page']? params['page']:1;
this.pageSize = params['pageSize']? params['pageSize']:10;
});
}
}
首先我认为这是因为 @Input 是在渲染组件之后加载的,但是即使我在构造函数中设置值或直接在变量声明中设置值,它也不起作用。
P.S.:pageSize 属性有效,和 currentPage 一样使用。
P.S.2:我不认为是实现ControlValueAccessor的情况,这应该是在ngx-bootstrap组件中实现的吧?
【问题讨论】:
-
对于ngModel指令,需要实现ControlValueAccessor...见骗子
-
我放弃了,切换到ng-bootstrap。
标签: angular pagination angular6 ngx-bootstrap