我已经看到了这个问题的一些答案并尝试了所有这些问题,但它仍然无法正常工作。我有点想用角度2写一个CMS;所以后端有人放置HTML和前端呈现所述HTML。我可以使用{% for datasource in datasources
%} {% for dst in datasource.target %}Target:{{ dst }} {% endfor %}{% endfor %}
显示一些HTML,但不能显示其他HTML。例如,我可以添加DomSanitizer.bypassSecurityTrustHtml()
,但<img class="center" src="">
呈现为纯HTML按钮。我还有一个<button md-button>test</button>
w / LessonComponent
选择器,但它在页面上根本没有呈现,我确实在检查器中看到了lesson
HTML。
如何(如果可能)在数据库中显示页面上的组件?代码如下:
lesson.ts
<lesson></lesson>
lesson.html
import { Component, Inject, Input, SecurityContext } from '@angular/core';
import { MdDialog, MD_DIALOG_DATA } from '@angular/material';
import { Lesson, LessonService } from '../lesson.service';
import { DomSanitizer, SafeHtml} from '@angular/platform-browser';
@Component({
selector: 'lesson',
templateUrl: './lesson.html',
styleUrls: ['./lesson.scss']
})
export class LessonComponent {
@Input() lessonID: number = 0;
@Input() questionID: string = '';
lesson: Lesson;
constructor(public service: LessonService, public dialog: MdDialog, private sanitizer: DomSanitizer) { }
getLesson(): object {
if(!this.service.getLesson(this.lessonID)) { return {}; }
let lesson = this.service.getLesson(this.lessonID)['pages'].filter(lesson => {
return lesson['id'] === this.questionID;
})[0];
if(lesson && lesson['buttons']) {
this.service.buttons = lesson['buttons'];
} else {
this.service.buttons = [];
}
lesson['test'] = this.sanitizer.bypassSecurityTrustHtml(lesson['content']);
return lesson;
}
doClick(btnID) {
let button = this.getLesson()['buttons'].filter(btn => {
return btn['id'] === btnID;
})[0];
this.dialog.open(ExtraDialog, { data: button['content'] });
}
}
@Component({
selector: 'extra-dialog',
template: `{{data}}`
})
export class ExtraDialog {
constructor(@Inject(MD_DIALOG_DATA) public data: any) {}
}
lesson.service.ts
<div *ngIf="!getLesson()" >
Error: Lesson {{lessonID}} -> question {{questionID}} is not a known lesson or question. Please click <a routerLink="/lesson/1">here</a> to go to the first lesson.
</div>
<div *ngIf="getLesson() && getLesson()['buttons']">
<span *ngFor="let button of getLesson()['buttons']">
<button md-mini-fab (click)="doClick(button.id)"><md-icon>{{button.icon}}</md-icon></button>
</span>
</div>
<div *ngIf="getLesson()">
<h2>{{getLesson()['title']}}</h2>
<p [innerHTML]="getLesson()['test']"></p>
</div>