【发布时间】:2016-12-21 04:36:25
【问题描述】:
我有一个 Angular 2 (RC5) 组件,它进行 HTTP 调用并将结果设置为组件的模板。我想将一个值注入到 HTTP 调用返回的 HTML 中。例如,返回的 HTML 中的其中一行是:
<a class="d2h-file-name" href="{{chapterURL}}">app/views/login/login.xml</a>
但是,它完全按原样呈现,没有注入 chapterURL。大概是因为初始化过程中没有设置模板吧?如果是这样,我应该如何将这些动态值注入到模板中?
这是组件。
@Component({
selector: 'codestep',
template: `<div class="codestep" [innerHTML]="content"></div>`
})
export class codeStepComponent {
@Input() step: string;
private content: string = '';
private chapterURL;
constructor(private route: ActivatedRoute, private http: Http) { }
ngOnInit() {
this.chapterURL = './diff/' + this.step + '.html';
this.getChapter()
.subscribe(
chapterContent => this.content = chapterContent,
error => this.errorMessage = <any>error);
}
getChapter(): Observable<any> {
return this.http.get(this.chapterURL)
.map(this.extractData)
.catch(this.handleError);
}
private extractData(res: Res) {
let body = res._body;
return body;
}
//Error handling function here...
}
编辑:
我已将 http 调用返回的源 html 文件更改为:
<a class="d2h-file-name" href={{chapterURL}}>app/views/login/login.xml</a>
然后将组件的模板改成:
template: `<div class="codestep" [innerHTML]="content|rawHtml"></div>`
其中rawHtml 是使用DomSanitizationService 上的bypassSecurityTrustHtml() 函数清理内容的管道,但是我仍然得到相同的结果,呈现的结果是:
<a class="d2h-file-name" href="gitURL">app/views/login/login.xml</a>
如果我对浏览器中选择的组件执行ng.probe($0),则返回的结果对象具有属性,但列出的唯一属性是innerHTML,仅此而已...
【问题讨论】:
-
在哪里使用了
step@Input 变量? -
@candidJ
this.step在构造url时使用 -
我的意思是说它是从哪里渲染的...任何父组件将值传递给步骤?
-
@candidJ 是的,该组件被称为例如
<codestep [step]="2.1"></codestep> -
<a class="d2h-file-name" [href]="chapterURL">app/views/login/login.xml</a>有帮助吗?
标签: javascript http angular typescript