【发布时间】:2017-10-18 17:46:26
【问题描述】:
我正在尝试更新从 API 调用以 html 形式返回的 observable。
我想知道是否有人可以帮助我。
html(在另一个组件上)
<common-content [theme]="theme" ></common-content>
组件是:
import { Component, OnInit, Input } from '@angular/core';
import { Http, Response } from '@angular/http';
import { ThemeModel } from '../../models';
import 'rxjs/add/operator/toPromise';
@Component({
selector: 'common-content',
template: `<div innerHTML = "{{innerHtml}}"></div>`
})
export class CommonContentComponent implements OnInit {
@Input() page: string;
@Input() theme: ThemeModel;
innerHtml: string;
constructor(private http: Http) {
}
ngOnInit() {
this.populatePage();
}
populatePage(){
let thisUrl = 'myPage.html';
this.http.get(thisUrl).subscribe(f => {
var content = <string>f['_body'];
this.innerHtml = content.replace("{{theme.Name}}", this.theme.name);
}, (error) => {
let e = error;
}, () => {
});
}
}
因此,observable 应该自动更新,而不是“替换”。
我尝试过使用订阅,也尝试过承诺,但我似乎无法让语法正常运行。
谁能帮忙?
提前致谢
【问题讨论】:
-
你能显示f的内容吗
-
只是一个芯片,而不是
<string> f['_body'];更改为<string>f.text()更安全 -
我不明白你在问什么。你想达到什么目的。您的代码发送一个 HTTP 请求,当响应返回时,使用响应的主体初始化组件的 innerHtml 字段(经过一些替换)。您还想实现什么目标?
-
在模板中使用 而不是
-
感谢 Theophilus,我使用 f.text() 代替。我试图实现 Chandru 是不需要对返回的 html 进行替换。它将它作为一个字符串返回,而不是作为一个可用的 observable。
标签: angular typescript angular2-template angular2-observables