问题
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