document setAttribute ngModel Angular

有些话、适合烂在心里 提交于 2021-02-19 19:44:12

问题


I am trying create an input element from typescript to html in angular 4. But when I try put [(ngModel)] not works. How I do that method ? Someone know how ?

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)));
  }

回答1:


Something like

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

is not supposed to work. Property/attribute bindings and component/directive instantiation only happens for markup added to a components template statically.

If it is added dynamically it won't have any effect. If you need this, you can create and compile a component at runtime and add that component dynamically (to a statically defined ViewContainerRef).

Alternatively you can use imperative code to update the DOM.



来源:https://stackoverflow.com/questions/46796404/document-setattribute-ngmodel-angular

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!