简体   繁体   English

文档setAttribute ngModel Angular

[英]document setAttribute ngModel Angular

I am trying create an input element from typescript to html in angular 4. But when I try put [(ngModel)] not works. 我正在尝试从[(ngModel)]到html创建一个input元素,但是当我尝试使用[(ngModel)]不起作用。 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)));
  }

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 ). 如果需要,可以在运行时创建和编译组件,然后将其动态添加(到静态定义的ViewContainerRef )。

Alternatively you can use imperative code to update the DOM. 或者,您可以使用命令性代码来更新DOM。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM