首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角度4:刷新div中的数据

角度4:刷新div中的数据
EN

Stack Overflow用户
提问于 2018-04-10 15:18:19
回答 1查看 4.2K关注 0票数 0

我有一个页面,在从REST服务获得数据DIVs后,在循环中显示数据DIVs,该服务返回一个列表。每个DIV只供一次检查。当我单击一个A+或A-以增加/减少数据后,我将调用另一个服务。现在,REST调用是从另一个组件执行的。REST服务只返回一次检查(而不是检查列表)。因此,在得到REST的响应后,我想更新我单击的检查中的计数

Main.component.html

代码语言:javascript
复制
<p>Filter Data goes here</p>

<div class="wrapper">
    <div class="box" *ngFor="let inspection of inspectionList let index=index; let odd=odd; let even=even">
        <app-inspection [inspect]="inspection"></app-inspection>
    </div>
</div>

Main.component.ts

代码语言:javascript
复制
ngOnInit() {
    this.http
        .get('api/inspection/1')
        .toPromise()
        .then(response => {
            let data = response.json();
            if (data.inspectionList) this.inspectionList = data.inspectionList;
        })
        .catch(error => {
        });
}

insp.component.html

代码语言:javascript
复制
<div class="row">
    <div> {{inspect.inspectionDescription}} </div> 
    <table>
        <tr>
            <td><a [routerLink]="" (click)="updateCount(inspect.id, 1, 'AM')" class="btn btn-success">A-</a></td>
            <td>{{inspect.rankAcount}}</td>
            <td><a [routerLink]="" (click)="updateCount(inspect.id, 1, 'AP')" class="btn btn-danger">A+</a></td>
        </tr>

现在是onClick of A+或A-,我调用一个POST服务(另一个component.ts中的)来更新数据库并返回结果。

insp.component.ts

代码语言:javascript
复制
updateCount() {
    this.http
        .post(url, params, {headers: this.headers})
        .toPromise()
        .then(response => {
            let data = response.json();
            if (data.rankAcount) alert(data.rankAcount);
        })
        .catch(error => {
        });
}

现在,我将如何在一次检查中更新计数,而不重新加载整个页面。

谢谢你提前帮忙!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-10 15:34:57

为什么您不增加rankAccount来检查updateCount,如果POST失败了,您只需要减少它;对于减量操作,就像单击和失败时的递减一样,您只需要将它增加回来。

代码语言:javascript
复制
incrementCount(inspect, modelId, rank) {
    inspection.rankAcount += rank;
    updateCount({
        inspectionId: inspect.id,
        modelId: modelId,
        rankChange: inspection.rankAcount
    }).then(() => {
        // etc.
    }).catch(() => {
        inspection.rankAcount -= rank;
    });
}

decrementCount(inspect, modelId, rank) {
    inspection.rankAcount -= rank;
    updateCount({
        inspectionId: inspect.id,
        modelId: modelId,
        rankChange: inspection.rankAcount
    }).then(() => {
        // etc.
    }).catch(() => {
        inspection.rankAcount += rank;
    });
}

updateCount(params) {
    this.http.post(url, params, {headers: this.headers}).toPromise();
}

或者像这样,但我会坚持三种方法。

代码语言:javascript
复制
updateCount(inspection, nr) {
    inspection.rankAcount += nr;
    this.http.post(url, params, {headers: this.headers}).toPromise().then(() => {
        // etc.
    }).catch(() => {
        inspection.rankAcount -= nr;
    });
}

updateCount(inspection, 1);
updateCount(inspection, -1);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49757338

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档