【问题标题】:How to change content of Mat-Label without refreshing the page如何在不刷新页面的情况下更改 Mat-Label 的内容
【发布时间】:2019-10-03 08:18:20
【问题描述】:

我有一个显示客户结束日期的垫子标签。 当我向 API 发出 GET 请求时,我最初会得到结束日期。 假设结束日期是 16-05-2099。我按原样显示结束日期。 现在我有一个删除按钮,它执行软删除。这意味着它不会删除详细信息,它只会将结束日期更改为当前日期,即今天的日期。

最初我会这样显示我的详细信息:

   <div *ngIf="showContact; else editableContact">
                        <div *ngFor="let element of sampleData1.contact">
                          <div *ngIf="contact.toString() === element.contactType" [attr.data-status]="element.contactStatus">
                            <br />

                            <mat-label hidden>Contact Id: {{ element.contactId }}</mat-label>
                            <mat-label>Contact Sub Type: {{ element.contactSubType }}</mat-label>
                            <br />
                            <mat-label>Contact Reference Type:{{ element.referenceNumber }} </mat-label>
                            <br />
                            <mat-label>Agreement Id : [</mat-label>
                            <mat-label *ngFor="let agreementIds of element.agreementId"> {{ agreementIds + ' ' }} </mat-label>
                            <mat-label>]</mat-label>
                            <br />

                            <mat-label>Contact Last Verified Date: {{ element.lastVerifiedDate | date: 'dd/MM/yyyy' }}</mat-label>
                            <br />
                            <mat-label>Contact End Date:{{ element.endDate | date: 'dd/MM/yyyy' }}</mat-label>
                          </div>
                        </div>
                      </div>

打字稿代码:

 deleteContact(contactId) {
const deleteCustomerId = this.customerId;
const deleteContactId = contactId;
this.customer360Service.deleteIndCustContact(deleteCustomerId, deleteContactId).subscribe(
  data => {
    console.log(data);
    this.snackbar.open('Contact Deleted Successfully', 'Close', {
      duration: 3000
    });
  },
  err => {
    this.snackbar.open('Failed To Delete Contact', 'Close', {
      duration: 3000
    });
  }
);

}

删除按钮 HTML:

 <button
                                style="float: right"
                                [hidden]="showContactDeleteButton"
                                mat-button
                                color="black"
                                matTooltip="Delete"
                                class="view-data"
                                (click)="deleteContact(element.contactId)"
                              >
                                <i class="fa fa-trash" aria-hidden="true"></i>
                              </button>

问题是我不需要在 HTML 中编写任何代码。我从后端获取数据我只需要显示它。我不必在 Typescript 或任何地方编写任何逻辑。最初我会从 API 获得一个结束日期,然后当我点击删除 API 时,API 会给我一个当前日期,我只需要显示它。

一切正常,但我面临的唯一问题是删除显示日期后没有改变。我必须刷新页面并再次从后端获取数据才能看到更改。如何在不刷新页面的情况下显示新日期。

【问题讨论】:

  • 你能贴出删除日期的代码吗
  • 我已经更新了我的问题,请看一下
  • 您是如何在 html 中获取元素的,它是在循环中还是在组件变量中?
  • @AsimHashmi 我向一个 API 发出了一个获取请求,它给了我结束日期。在 html 中,我使用循环并显示它 element.startDate,element.endDate, element.address

标签: javascript html angular typescript rest-client


【解决方案1】:

如果数据包含联系人所需的所有数据,并且取决于变量的结构和订阅返回的内容,这应该会更新您的数据并刷新您的视图。

 deleteContact(contactId) {
const deleteCustomerId = this.customerId;
const deleteContactId = contactId;
this.customer360Service.deleteIndCustContact(deleteCustomerId, deleteContactId).subscribe(
  data => {
    console.log(data);
    this.sampleData1.contact = data; // add this
    this.snackbar.open('Contact Deleted Successfully', 'Close', {
      duration: 3000
    });
  },
  err => {
    this.snackbar.open('Failed To Delete Contact', 'Close', {
      duration: 3000
    });
  }
);

【讨论】:

  • 因此,您在 html 模板中迭代变量 sampleData1.contact。根据 sampleData1 和 sampleData1.contact 是哪种结构,我猜后者是一个数组,您可以尝试在console.log(data); 行下方添加this.sampleData1.contact = data;
【解决方案2】:

当您的删除函数成功完成对 API 的请求时,您需要更新数组中的对象。现在要更新对象,您需要一些独特的属性来识别数组中的对象。为此,您可以使用对象索引

现在假设您的数组名称是 dates,您在循环中使用它

<div *ngFor="let element of sampleData1.contact; let i = index">
  <mat-label>Contact End Date:{{ element.endDate | date: 'dd/MM/yyyy' }}</mat-label>
  <button (click)="deleteAddress(element.addressId, i)" </button> //i will be the index of element inside array
</div>

然后在你的删除函数中

deleteAddress(addressId, index) {
const deleteCustomerId = this.customerId;
const deleteAddressId = addressId;
this.customer360Service.deleteIndCustAdd(deleteCustomerId, deleteAddressId).subscribe(
  data => {
    console.log(data);
    // here you can delete the whole object or update it

    this.sampleData1.contact[index].endDate = "You new Date" // <- this will his the object and auto updates the view without reloading
    this.snackbar.open('Address Deleted Successfully', 'Close', {
      duration: 3000
    });
  },
  err => {
    this.snackbar.open('Failed To Delete Address', 'Close', {
      duration: 3000
    });
  }
);
}

【讨论】:

  • 我不需要拼接数据。我只是在做一个软删除。结束日期将从将来的某个时间更改为当前日期。仅此而已。对象仍然存在。假设最初的结束日期是 2020 年 5 月 16 日,然后单击删除它将更改为当前日期。
  • 我已经更新了我的答案,看看 :) 你可以更新那个特定索引处的对象
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-09
  • 2011-06-15
相关资源
最近更新 更多