【问题标题】:No view refresh after copying items复制项目后没有视图刷新
【发布时间】:2021-11-03 22:25:52
【问题描述】:

我有一个密码。单击按钮后,它会复制(某个视图的)内容,然后移动到复制项目所在的另一个子页面。问题是复制的元素只有在页面刷新后才会显示。谁能帮我解开这个谜团?

duplicate(){
this.landingPageService.getLandingPageItem(this.landingPageData.id).subscribe(
      next => {
        this.loading = true;
        const copiedLandingPage = LandingPage.LandingPageFromDefinition(next),
          copyLandingPage = LandingPage.GenerateCreationDTO(
            copiedLandingPage.content,
            `${copiedLandingPage.name} (copy)`
          );
        copyLandingPage.lpType = copiedLandingPage.lpType;
        copyLandingPage.lastUpgraded = copiedLandingPage.lastUpgraded;
        this.landingPageService
          .createLandingPageItem(copyLandingPage)
          .subscribe(
            () => {
              this.toastGenerator.lpContentCreation.success();
              this.refreshSubject.next();
            },
            () => {
              this.loading = false;
              this.toastGenerator.lpContentCreation.failure();
            }
          );
      },
      () => {
        this.toastGenerator.lpContentCreation.failure();
      }
    );
      this.router.navigate(['/landing-page']);
  }
<button  (click)="duplicate()">Kopiuj</button>

【问题讨论】:

  • 在 Angular 中不要在实时页面上更新,因为它已经加载你应该用any 在事件监听器中写这个,这样它会自动更新
  • 拜托,再写点东西,举个例子?
  • 和hostListener有关吗?
  • 不,我在谈论事件侦听器,等等我在答案中粘贴代码

标签: angular


【解决方案1】:
let element = document.getElementById("Element-id");
    if (element) {
      element.addEventListener('scroll', (event) => {
        this.function(event);

function(event: any):void{
        this._rest.all(`/classrooms/`)
            .getList({ page: this.pageNumber+1, is_archived : false })
            .toPromise()
            .then((element) => {
              this.loadingElement = false
            }, (error) => {
              this.loadingElement = false;
              console.log("ERROR GETTING LIST", error);
          });
        }
      }

在上面的代码中,显示在显示上执行的任何事件,例如鼠标移动、按钮单击、悬停、刷新任何内容,它将自动调用该函数,并且您的页面会自动显示您在函数中给出的数据

例如上面的 eventlistner 将调用函数并且函数按照我们的指示完成它的工作

you can see docs here

【讨论】:

  • 什么意思?它与问题有何关系?在 Angular 中,您不会使用 addEventListener 添加事件侦听器。至少有 3 个更好的选项,例如:fromEvent、HostListener(如果事件源应该附加到组件 - 如果没有,您仍然可以使用它,但您需要创建指令并将其附加到 HtmlElement)或只是正如文档所说 - EventBinding。在 Angular 中,您可以使用 RouterModule(路由器、routerLink 等)进行导航。另外 - 最好坚持 Obsevables - 它们更强大(我在这里谈论的是 HTTP 调用)
  • 请参阅我尚未添加应用事件侦听器的部分添加以供参考,请参阅此stackoverflow.com/questions/41609937/…
猜你喜欢
  • 2019-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多