简体   繁体   English

在angular2中重新加载div

[英]Reload div in angular2

Im calling an api in my html component.我在我的 html 组件中调用了一个 api。 But I need to refresh it each time to be able to see the data.但是我每次都需要刷新它才能看到数据。

html component: html组件:

<div>
<div *ngIf="reloadiv" class='tableauPlaceholder' style='width: 1353px; height: 595px;'>
<object class='tableauViz' width='1353' height='595' style='display:none;'>
<param name='host_url' value='https%3A%2F%2Fmbi-tab.abc.com%2F' />
<param name='site_root' value='&#47;t&#47;IT' />
<param name='name' value='TPO&#47;ModelOutputMMX' /><param name='tabs' value='no' />
<param name='toolbar' value='yes' /><param name='showShareOptions' value='true' />
</object></div>

</div>

component.ts:组件.ts:

import { Component, OnInit } from '@angular/core';


@Component({
selector: 'model1',
styleUrls: ['./model1.scss'],
templateUrl: './model1.html'
})


export class model1 implements OnInit {
private reloadiv: boolean = false;
ngOnInit() {
console.log("Init invoked");
this.reloadiv = true;
}

}

How do I reload div without refreshing the entire page?如何在不刷新整个页面的情况下重新加载 div?

In order to update the template with the latest data, first import ChangeDetectorRef为了使用最新数据更新模板,首先导入 ChangeDetectorRef

import {ChangeDetectorRef} from '@angular/core';

initialize it in your constructor在你的构造函数中初始化它

constructor(private cd : ChangeDetectorRef){}

and in the success part of your API call subscribe, once you've updated your variables, manually trigger template change detection with并且在您的 API 调用订阅的成功部分,一旦您更新了变量,请手动触发模板更改检测

  this.cd.detectChanges();

This should do the trick if because of any reason your change detection is not occuring automatically.如果由于任何原因您的更改检测没有自动发生,这应该可以解决问题。

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

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