【发布时间】:2020-06-09 23:55:33
【问题描述】:
我实际上正在开发一个角度应用程序,我必须将一个 [innerHTML] 元素放在一个 div 中。
我的代码
像这样:
something.component.html
<section class="mx-auto" *ngFor="let publication of publication">
<div [innerHTML]="publication.content"></div>
</section>
所以在 ts 中:
something.component.ts
import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import { Subscription } from 'rxjs';
import { ActivatedRoute } from '@angular/router';
import { Title, Meta } from '@angular/platform-browser';
import { Publication } from '../publication.model';
import { PublicationsService } from '../publication.service';
@Component({
selector: 'app-free-publication',
templateUrl: './something.component.html',
styleUrls: ['./something.component.scss'],
encapsulation: ViewEncapsulation.None
})
export class FreePublicationComponent implements OnInit {
publication: Publication[] = [];
suggestions: Publication[] = [];
private routeSub: Subscription;
getId: any;
isLoading = false;
constructor(public publicationsService: PublicationsService, private route: ActivatedRoute, private titleService: Title, private meta: Meta) {
this.getId = this.route.url['_value'][1].path;
this.getId = + this.getId;
}
ngOnInit() {
this.isLoading = true;
// main publication
this.routeSub = this.route.params.subscribe(params => {
this.publicationsService.getPublication(params['publicationId']).then(dataPublication => {
for (let i = 0; (dataPublication.content.match(/wp-content/g) || []).length; i++) {
dataPublication.content = dataPublication.content.replace('https://aurelienbamde.com/wp-content/', 'assets/content/');
}
this.titleService.setTitle(dataPublication.title);
this.meta.addTag({ name: 'keywords', content: dataPublication.post_tag });
this.publication = [dataPublication];
});
});
}
}
而且我的 innertHTML 不会返回我发送的 html 文档的样式。
我的测试
使用 ngOnInit 末尾的 console.log(),我可以看到我的 html 包含所有样式属性,但是通过检查 innerHTML 的 div,里面没有样式。
我的问题
所以我很好地实现了ViewEncapsulation.None,正如你所看到的,对其他元素有一个操作,所以它有效,但不适用于我的 innerHTML。
你有什么想法,版本问题?还是与其他元素合作?
提前感谢您的宝贵时间!
祝你在项目中取得成功。
【问题讨论】:
标签: html css angular typescript innerhtml