【发布时间】:2017-08-08 11:45:35
【问题描述】:
我希望我的 Angular 应用能够在移动设备上显示带有点击事件的脚注对于使用 nativeElement.innerHTML 注入的文本。我认为我在第一次尝试时没有很好地解释它,所以我会再试一次。我实际上已经阅读了有关模板和数据绑定的内容。问题是当文本被注入时它不起作用 nativeElement.innerHTML 所以我需要一些其他的方法来做到这一点。这是一些代码和一个失败的 plunkr。
import {Component, NgModule, VERSION, ViewChild, ElementRef} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
@Component({
selector: 'my-app',
template: `
<div>
<h2>Hello {{name}}</h2>
<button (click)="toggleFootNotes()">toggle</button>
<p>Text in template:
And when
<sup>a
<span *ngIf=this.showFootnotes> testing 1 2 3 </span>
</sup>
he came ...
</p>
<div #textwithfootnote></div>
</div>
`,
})
export class App {
@ViewChild('textwithfootnote') textwithfootnote: ElementRef;
name:string;
public showFootnotes: boolean = false;
constructor() {
this.name = `Angular! v${VERSION.full}`
}
public toggleFootNotes() {
this.showFootnotes = !this.showFootnotes;
}
ngAfterViewInit() {
this.textwithfootnote.nativeElement.innerHTML = " \
\n<p>Text injected: \
\n And when \
\n <sup>a \
\n <span *ngIf=this.showFootnotes> testing 1 2 3 </span> \
\n </sup> \
\n he came ... \
\n </p>";
}
}
@NgModule({
imports: [ BrowserModule ],
declarations: [ App ],
bootstrap: [ App ]
})
export class AppModule {}
http://plnkr.co/edit/wY2vRt4dcMHq12SlJSmv
有没有办法做到这一点?
谢谢!
【问题讨论】:
标签: javascript jquery html angular ionic3