我有一個內部有form
元素的組件。此表單有一個input[type="number"]
,它將用於調整iframe
元素。問題是,iframe
元素在@ViewChild
之內。如何將[(ng-model)]
輸入到[width]
的iframe
裏面@ViewChild
?該代碼看起來是這樣的:如何在Angular4中將[(ng-model)]傳遞給@ViewChild?
@Component({
selector: "demo-iframe",
template: `
<form (submit)="onSubmit($event)">
<input type="number" min="0" [(ngModel)]="model.px">px
<button>update px</button>
</form>
<div #placeholder></div>
`
})
export class DemoIframeCmp {
@ViewChild("placeholder", { read: ViewContainerRef }) private viewContainerRef: ViewContainerRef;
constructor (
private route: ActivatedRoute,
private router: Router,
private compiler: Compiler
) {
document.title = "Test iframe";
this.paramsSub = this.route.params.subscribe((params: IClickDemoParams) => {
document.title = `Demo iframe ${params.demoId}`;
this.buildDemo(params.demoId);
});
this.setWidth = 800;
}
private buildDemo (selector: string) {
@Component({
selector: "test-iframe",
template: `
<iframe class="iframe-demo" src="http://localhost:3000/${selector}" [width]="px"></iframe>
`
})
}
你可以給一個快速演示?嘗試它,但無法通過價值:( – ivanasetiawan
這是演示。您可以使用html符號將值傳遞給您的孩子:[yourValue] =「value」 – Carsten