我想更新從API調用中返回的html中的observable。通過http.get觀察角4更新
我想知道是否有人可以幫助我。
的HTML(另一個部件)
<common-content [theme]="theme" ></common-content>
和組件是:
所以不是做一個 「替換」 可觀察到的應該只是自動更新。
我試過使用一個訂閱,我也試過了一個承諾,但是我似乎沒有能夠獲得語法的行爲。
任何人都可以幫忙嗎?
在此先感謝
我想更新從API調用中返回的html中的observable。通過http.get觀察角4更新
我想知道是否有人可以幫助我。
的HTML(另一個部件)
<common-content [theme]="theme" ></common-content>
和組件是:
所以不是做一個 「替換」 可觀察到的應該只是自動更新。
我試過使用一個訂閱,我也試過了一個承諾,但是我似乎沒有能夠獲得語法的行爲。
任何人都可以幫忙嗎?
在此先感謝
我建議您更新<string> f['_body'];
更改爲<string>f.text()
,也innerHTML = "{{innerHtml}}"
到[innerHTML]="view"
反正檢查以下plnkr鏈接,因爲它是做你正在試圖執行
this._http.get(link).subscribe(f => {
this.loading = false;
var content = <string>f.text();
this.view = content.replace("{{theme.Name}}", this.theme.name);
}, (error) => {
this.loading = false;
console.error(error);
alert(error);
});
的到底是什麼模板是這樣的
content <button (click)="open('external.html')">Open Page</button>
<strong *ngIf="loading">LOADING...</strong>
<div [innerHTML]="view"></div>
external.html
是s imple如下
me playing around with this theme with name
<b>
{{theme.Name}}
</b>
這裏是運行Plnkr
但對於字符串插值處理,如果內容是相同的模板,因爲父加載它,並綁定this
到模板的範圍,這是類似角1 NG-包括檢查此answer,因爲它有助於在解決了(而不是重新做的話),並注意這是角4和上述
使用Angular 4.0.0-beta.6's ngComponentOutlet。
感謝您的反饋Theophilus,不幸的是,當我使用「[innerHTML] =」view「」,如上所述,我在視圖上獲得{{theme.Name}}而不是數據,所以我仍然需要假裝我使用「替換」命令執行插值,而我更喜歡可觀察的句柄替換,就像它通常那樣。因此,我不想看到{{theme.Name}},而是想看到「黃色主題」(或者我在配置文件中輸入的任何內容)。 – Mick
檢查plnkr鏈接,它將替換它,還是試圖使加載的innerHTML內容像'ng-include'一樣工作? –
是的。這個想法是我有多個主題。我有一些在這些主題中很常見的文件。所以我想加載組件,而不加載共同的主題,因爲我想使用原始主題而不是改變它。 – Mick
1)你想要達到的目標還不清楚。 我可以做的是成功,你想更新dom。 2)不要使用內部html,並使用插值或ngModel與殺菌劑相同。 3)另一種方法是爲同一個創建一個自定義的可重用指令。
方式可能是:
1)使管道的消毒:
import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
/**
*
* @export
* @class SafeHtmlPipe
* @implements {PipeTransform}
*/
@Pipe({
name: 'safeHtml'
})
export class SafeHtmlPipe implements PipeTransform {
/**
*
* @param {DomSanitizer} sanitizer
* @memberof SafeHtmlPipe
*/
constructor(private sanitizer: DomSanitizer) { }
/**
*
* @param {any} style
* @returns
* @memberof SafeHtmlPipe
*/
transform(style) {
// return this.sanitizer.bypassSecurityTrustStyle(style);
return this.sanitizer.bypassSecurityTrustHtml(style);
// return this.sanitizer.bypassSecurityTrustXxx(style); - see docs
}
}
2)使用它像:
<div class="card_description" [innerHTML]="scenarioStepDataDesc | safeHtml"></div>
其中scenarioStepDataDesc是你的HTML內容。
3)使用管道和其它可重用的組件共享模塊/指令
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { MaterialModule } from '../material/material.module';
import { BlockUIModule } from 'ng-block-ui';
import { AutoCompleteComponent } from './components/autoComplete/autoComplete.component';
import { DialogDataComponent } from './components/dialog/dialog.component';
import { SafeHtmlPipe } from './pipes/safeHtml.pipe';
/**
*
* @export
* @class SharedModule
*/
@NgModule({
imports: [CommonModule, FormsModule, MaterialModule, BlockUIModule, ReactiveFormsModule],
exports: [
CommonModule,
FormsModule,
MaterialModule,
BlockUIModule,
ReactiveFormsModule,
AutoCompleteComponent,
DialogDataComponent,
SafeHtmlPipe
],
declarations: [AutoCompleteComponent, DialogDataComponent, SafeHtmlPipe]
})
export class SharedModule { }
享受:)
你能證明F的 – Carsten
只是在一個芯片上的內容,而不是' F [ '_body'];'改爲' f.text()'更安全 –
我不明白你在問什麼。你想要達到什麼目的?你的代碼發送一個HTTP請求,當響應返回時,用響應的主體初始化組件的innerHtml字段(在替換之後)。你還想要做什麼? –