【问题标题】:Angular Saving an Edited Content角度保存编辑的内容
【发布时间】:2021-07-19 16:56:11
【问题描述】:

我正在编写一个代码,用户可以在其中更改屏幕上的段落并将其保存。目前,用户可以更改段落,save() 操作有效。但是保存后,更改的段落不会通过网络。该段落不会更改,它会保存未经编辑的版本,就好像我什么都没写一样。我应该改变什么来实现这一点?

HTML:

  <div class="content fuse-white ml-24 mr-8 h-100-p" fusePerfectScrollbar>
            <div class="label-list">
                <p>
                    A Paragraph: 
    
                    <span contenteditable="true">
                        {{_stickerData?.StickerData}}
                    </span>
                </p>
              
            </div>
        </div>

TS:

save(){
    this.confirmDialogRef = this._dialog.open(FuseConfirmDialogComponent, {
        disableClose: false,
    });
    
    this.confirmDialogRef.componentInstance.confirmMessage =
    "The paragraph will be changed";

this.confirmDialogRef.afterClosed().subscribe((result) => {
    if (result) {
        this._productionService
            .saveStickerData(this._stickerData)
            .subscribe((response: IStickerData) => {
                this._stickerData = response;
                this._messages.Show(            
                    "SUCCESS",
                    3
                );
            });
    }
});
}

服务 TS:

saveStickerData(data: IStickerData): Observable<IStickerData> {
        return this._http.post("Production/SaveStickerData", data);
    }

【问题讨论】:

    标签: javascript html angular typescript


    【解决方案1】:

    [已编辑] 当您在模板中插入变量并使用 contenteditable - 它不会更新您的变量。您应该手动更新它。

    <span contenteditable [textContent]="_stickerData?.StickerData" (input)="onStickerDataChange($event.target.innerHTML)">
    </span>
    

    在 TS 中应该是这样的:

    onStickerDataUpdate(data) {
        this._stickerData.StickerData = data;
    }
    

    有用的链接:1, 2 [旧建议]

    1. 您确定回复已编辑段落吗?如果是,则更改检测可能存在问题。在组件构造函数中导入ChangeDetectorRef,获取数据时触发markForCheck()方法。
    2. 嵌套订阅是一种不好的做法,请尝试使用 switchMap() 进行重构

    这是一个伪代码来展示这个想法:

       class MyComponent {
          constructor(private cd: ChangeDetectorRef) {}
            
           save(){
             this.confirmDialogRef = 
             this._dialog.open(FuseConfirmDialogComponent, {
                disableClose: false,});
             this.confirmDialogRef.componentInstance.confirmMessage =
        "The paragraph will be changed";
    
             this.confirmDialogRef.afterClosed().pipe(switchMap(result) => {
               if (result) {
                 return this._productionService.saveStickerData(this._stickerData);
               } else {
                 return EMPTY;
               }
            }).subscribe((response) => {
              this._stickerData = response;
              this._messages.Show(            
                 "SUCCESS",
                 3
              );
              this.cd.markForCheck();
            });
          }
       }
    

    【讨论】:

    • 谢谢,我将 ChangeDetectorRef 添加到我的代码中,但它仍然没有保存编辑后的文本。
    • @RocketMonkey。您是否检查过您是否将准确编辑的数据发送到您的生产服务?服务也响应了编辑过的数据,而不是之前的数据?
    • @RocketMonkey。似乎 contenteditable 不会改变你的变量
    • 我做到了。是的,我发送的数据和服务的响应是正确的。但是我应该怎么做才能使 contenteditable 工作?
    • 谢谢你,你太棒了。现在它保存编辑的文本。刷新页面时我看不到新文本。我应该在 HTML 中添加一些东西吗?
    猜你喜欢
    • 2019-04-07
    • 2019-03-09
    • 1970-01-01
    • 2019-05-21
    • 1970-01-01
    • 1970-01-01
    • 2018-02-06
    • 2018-08-19
    • 2021-09-27
    相关资源
    最近更新 更多