简体   繁体   English

角度 6 中的多个承诺中的承诺?

[英]Promise inside the multiple promises in angular 6?

Actual Problem : Pagination is not showing in p-table .实际问题p-table未显示分页。

Solution : We have to store all the data at once then Pagination will display.解决方案:我们必须一次存储所有数据,然后分页才会显示。 For this I am trying this method为此,我正在尝试这种方法

Actually I am upgrading the p-Datatable to p-table in ngPrime.实际上我正在将 p-Datatable 升级到 ngPrime 中的 p-table。 In p-datatable is loading with pagination, but when I upgrading the Datatable to table pagination is not showing.在 p-datatable 中加载分页,但是当我将 Datatable 升级到表分页时没有显示。

I looked on it.我看着它。 If I make the settimeout pageination is showing, but we can't use the settimeout(becuase I don't the no process and responsoe time).如果我使 settimeout 分页显示,但我们不能使用 settimeout(因为我不知道无进程和响应时间)。

So I have tried with promises, but still I can't able to make the each single promises.所以我尝试过承诺,但我仍然无法做出每一个承诺。

My scenario is First I am making the one response using the response I am making the multiple responses and storing the data into array.我的场景是首先我使用响应做出一个响应,我做出多个响应并将数据存储到数组中。 Finally I am using that array in p-table template.最后我在 p 表模板中使用该数组。 Below is my code下面是我的代码

allArr = [];
allTemp = [];

ngOnInit():void{
    this.getVal.then((x)=>{
        this.allArr = x;
    })
}

getVal = new Promise( (resolve,reject)=>{    
    let resource = "path/name";
    this.log.getList(resource).subscribe(
    rst=>{
        for(var key in rst){
            this.getInfo(key.name, key.value);
        }
        //here I don't know where I have to resolve the promise

        //setTimeout(() => {
        //    resolve(this.serviceTemp);
        //},2000);

    })
}


getInfo(name, value): void{
    this.log.getDetail(name)
    .subscribe(
        (log: any) => {
            this.allTemp.push(log.detail);
        });
}

My template code我的模板代码

<p-table [columns]="tableHeader" [style]="{'width':'100%'}" [value]="allArr" selectionMode="single" [(selection)]="selectedService" [paginator]="true" [rows]="10" [rowsPerPageOptions]="[5,10,25]" 
[responsive]="true" sortField="serviceName" sortOrder="1" sortMode="multiple">

Got the solution.得到了解决方案。 Actually I want to make the promise all, for the inner subscribe not for the first calling function其实我想做出所有的承诺,对于内部订阅而不是第一个调用函数

allArr = [];
allTemp = [];

ngOnInit():void{
  getVal();
}
allPromise = [];

getVal(){    
    let resource = "path/name";
    this.log.getList(resource).subscribe(
    rst=>{
        for(var key in rst){
            allPromise.push(this.getInfo(key.name, key.value));
        }
        Promise.all(this.allPromise).then(()=>{
            this.allArr = this.allTemp;
        });
    })
}


getInfo(name, value): void{
    let _this = this;
    return new Promise(function(resolve, reject) {
        _this.log.getDetail(name)
        .subscribe(
            (log: any) => {
                this.allTemp.push(log.detail);
            });
    })
} 

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

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