簡體   English   中英

文檔setAttribute ngModel Angular

[英]document setAttribute ngModel Angular

我正在嘗試從[(ngModel)]到html創建一個input元素,但是當我嘗試使用[(ngModel)]不起作用。 我該怎么做? 有人知道嗎?

createElement() {
    this.input = document.createElement('input');
    this.input.setAttribute('matInput', '');
    this.input.setAttribute('placeholder', 'Tema');
    this.input.setAttribute('[(ngModel)]', 'module.theme');
    this.input.setAttribute('name', 'theme');

    return (<HTMLDivElement>(document.getElementById('ejemplo').appendChild(this.input)));
  }

就像是

 this.input.setAttribute('[(ngModel)]', 'module.theme');

不應該工作。 屬性/屬性綁定和組件/指令實例化僅針對靜態添加到組件模板的標記發生。

如果動態添加,它將沒有任何效果。 如果需要,可以在運行時創建和編譯組件,然后將其動態添加(到靜態定義的ViewContainerRef )。

或者,您可以使用命令性代碼來更新DOM。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM