问题描述
我试图从输入脚本创建一个输入元素,从输入脚本到Angular 4中的html.但是当我尝试输入 [(ngModel)]
时,它不起作用.我该怎么做?有人知道吗?
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)));
}
推荐答案
类似
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.
如果它是动态添加的,则没有任何作用.如果需要,可以在运行时创建和编译组件,然后将其动态添加(到静态定义的 ViewContainerRef
).
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
).
或者,您可以使用命令性代码来更新DOM.
Alternatively you can use imperative code to update the DOM.
这篇关于文档setAttribute ngModel Angular的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!