简体   繁体   English

如何在不刷新页面的情况下更改Mat-Label的内容

[英]How to change content of Mat-Label without refreshing the page

I have a mat-label which displays Customer End Date. 我有一个显示客户结束日期的垫标签。 I get the end date initially when I do a GET request to an API. 当我向API发出GET请求时,我最初得到结束日期。 Suppose the end date is 16-05-2099.I display the end date as it is. 假设结束日期是16-05-2099。我按原样显示结束日期。 Now I have a delete button which does a soft delete.It means that it will not remove the Details it will just change the end date to current date ie to today's date. 现在我有一个删除按钮,它执行软删除。这意味着它不会删除详细信息它只会将结束日期更改为当前日期,即今天的日期。

Initially I display my Details like this: 最初我显示我的详细信息,如下所示:

  <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> 

Typescript Code: 打字稿代码:

 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
    });
  }
);

} }

Delete Button HTML: 删除按钮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>

The thing is I don't need to write any code in HTML.I am getting data from backend I just need to display it.I don't have to write any logic in Typescript or anywhere. 问题是我不需要在HTML中编写任何代码。我从后端获取数据我只需要显示它。我不必在Typescript或任何地方编写任何逻辑。 Initially I will get an End Date from API and then when I hit a delete API the API will give me a current date which I just have to display. 最初我将从API获得结束日期,然后当我点击删除API时,API会给我一个我必须显示的当前日期。

Everything works fine, however the only issue I am facing is that after deleting the display date does not change.I have to refresh the page and fetch data again from backend to see the changes. 一切正常,但我面临的唯一问题是删除显示日期后不会改变。我必须刷新页面并从后端再次获取数据以查看更改。 How can I display new date without refreshing the page. 如何在不刷新页面的情况下显示新日期。

You need to update the object in array when your delete function successfully completes the request to the API. 当delete函数成功完成对API的请求时,您需要更新数组中的对象。 Now to update object, you need some unique property to identify object in array. 现在要更新对象,您需要一些唯一的属性来标识数组中的对象。 For that purpose, you can use index of object 为此,您可以使用对象的索引

Now assuming your array name is dates which you are using inside the loop 现在假设您的数组名称是您在循环中使用的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>

Then inside your delete function 然后在你的删除功能

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
    });
  }
);
}

Provided the data includes all data you need for your contact and it depends on the structure of your variable and what subscribe returns, this should update your data and refresh your view. 如果数据包含联系人所需的所有数据,并且这取决于变量的结构和订阅返回的内容,则应更新数据并刷新视图。

 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
    });
  }
);

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

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