1
votes

I am using JavaScript API to create html elements from JSON Schema. When I passed the json schema, it's returns widget html element object.

Widget is HTML DOM object which contains tagName, id. So for two data binding I have include [(ngModel)] into dom object, for this I am using-

widget.setAttribute("[(ngModel)]", "model");

But It's giving me error-

[(ngModel)]' is not a valid attribute name

Inside @NgModule, I have already included BrowserModule and FormsModule

1
Please post some code. To me it's entirely unclear what "I want to apply [(ngModel)] for two way data binding using setAttribute() function" could mean. You probably need to add BrowserModule, (or CommonModule) and FormsModule to imports of your current module. - Günter Zöchbauer
@GünterZöchbauer thnx for reply, I have update question - deen

1 Answers

1
votes

Angular bindings and component/directive instantiation is only happening for markup added statically to a components template. [] or () are never added to the DOM by Angular2 and Angular2 doesn't care about these being added to the DOM by other means. Bindings are processed by Angular before it adds HTML to the DOM

There is a way to add/remove components dynamically to the DOM using ViewContainerRef.createComponent() but that's it. (for an example see Angular 2 dynamic tabs with user-click chosen components)