我有一个文件上传表和一个带有innerHTML属性绑定的空div.当用户上传一个文件(EXCEL)时,它将被服务器处理,转换成html并存储在数据库中.然后,Angel将从数据库中获取数据,对其进行清理(这样就不会删除CSS样式),并通过innerHTML属性绑定将其插入到空div中.

问题是,在文件上传之后,如果不重新加载页面,它不会更新div的内容.我想在不重新加载页面的情况下更新<div *ngIf="budgetHTML" [(innerHTML)]="budgetHTML"></div>.

我是个新手,有什么建议可以让我做到这一点吗?

component.html

<div *ngIf="budgetHTML" [(innerHTML)]="budgetHTML">
</div>

Component.ts(我只包含了相关代码)

export class ProgramProjectInformationComponent implements OnInit, OnDestroy {

  budgetHTML: SafeHtml;

  constructor(
    private store: Store<AppStateInterface>,
    private formBuilder: UntypedFormBuilder,
    private grantProposalService: GrantProposalService,
    private route: ActivatedRoute,
    private sanitizer: DomSanitizer,
  ) {}

  ngOnInit(): void {
    this.initializeForm();
    this.initializedValues();
    this.initializePdoPrograms();
    this.initializePdoBankAccounts();
    this.intializeListeners();
    this.initializeBudgetTable();
  }

  initializedValues() {
    this.grantProposalData$ = this.store.pipe(select(grantProposalSelector));
    this.isLoading$ = this.store.pipe(select(isFetchingGrantProposalSelector));
    this.composeBankAccount$ = this.store.pipe(select(grantProposalComposeBankAccountSelector));
    this.grantProposalDraftStatus$ = this.store.select(grantProposalDraftStatusSelector);
  }

    initializeBudgetTable() {
        this.grantProposalData$
        .pipe(
            takeUntil(this.destroy$)
        )
        .subscribe(data => {
      this.budgetHTML = this.sanitizer.bypassSecurityTrustHtml(data?.budget_info);
    });     
    }

  /**
   * for importing budget data to database
   *
   * @return void
   */
  importBudget() {
    this.loadingUpload = true;
    
    if (this.budgetForm.valid) {      
      // convert formControl to FormData to allow sending images/files
      let formData = this.convertToFormData();      
      
      this.grantProposalId$.pipe(takeUntil(this.destroy$)).subscribe(id => {
        formData.set('grant_proposal_id', id);
      });
      
      this.grantProposalService
        .importBudgetTable(formData)
        .pipe(
          takeUntil(this.destroy$)
        )
        .subscribe({
          next: (res) => {
            this.showAlertDataSaved(res.results.message);
            this.initializedValues();
            this.initializeBudgetTable();
          },
          error: (err) => {
            this.showAlertSavingFailed(err.error.message);
            this.loadingUpload = false;
          },
          complete: () => {
            this.loadingUpload = false;
          }
        })
    } else {
      this.loadingUpload = false;
      this.showAlertCheckFields('Please fill in the required fields to proceed.');
    }
  }
}

推荐答案

  1. 确保在您从API获得响应之后,该流this.grantProposalData$ = this.store.pipe(select(grantProposalSelector));发出一个新值.
  2. 在发送formData之前,请等待设置grant_proposal_id.
  3. 你不必每次打importBudget都需要takeUntil(this.destroy$),你可以用first代替.

因此,请try 以下操作:

importBudget() {
  this.loadingUpload = true;

  if (this.budgetForm.valid) {
    // convert formControl to FormData to allow sending images/files
    const formData = this.convertToFormData();

    this.grantProposalId$
      .pipe(
        first(),
        switchMap(id => {
          formData.set('grant_proposal_id', id);

          return this.grantProposalService.importBudgetTable(formData).pipe(first());
        }),
      )
      .subscribe({
        next: res => {
          this.showAlertDataSaved(res.results.message);
          // Remove these 2 lines and make sure you assign a new value for grantProposalSelector
          // this.initializedValues();
          // this.initializeBudgetTable();
        },
        error: err => {
          this.showAlertSavingFailed(err.error.message);
          this.loadingUpload = false;
        },
        complete: () => {
          this.loadingUpload = false;
        },
      });
  } else {
    this.loadingUpload = false;
    this.showAlertCheckFields('Please fill in the required fields to proceed.');
  }
}

Javascript相关问答推荐

使用TMS Web Core中的HTML模板中的参数调用过程

我在这个黑暗模式按钮上做错了什么?

如何从JSON数组添加Google Maps标记—或者如何为数组添加参数到标记构造器

微软Edge编辑和重新发送未显示""

用JavaScript复制C#CRC 32生成器

从Node JS将对象数组中的数据插入Postgres表

如何在模块层面提供服务?

如何通过使用vanilla JS限制字体大小增加或减少两次来改变字体大小

JS,当你点击卡片下方的绿色空间,而它是在它的背后转动时,

Angular 中的类型错误上不存在获取属性

使用ThreeJ渲染的形状具有抖动/模糊的边缘

在WordPress中使用带有WPCode的Java代码片段时出现意外令牌错误

如何在使用rhandsontable生成表时扩展数字输入验证?

JavaScript是否有多个`unfined`?

将Node.js包发布到GitHub包-错误ENEEDAUTH

有没有一种直接的方法可以深度嵌套在一个JavaScript对象中?

按特定顺序将4个数组组合在一起,按ID分组

未捕获的不变违规:即使在使用DndProvider之后也应使用拖放上下文

$GTE的mongoose 问题

输入数据覆盖JSON文件