deen
deen

Reputation: 2315

setAttribute in Angular2 for two way data binding

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

Upvotes: 1

Views: 1509

Answers (1)

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657957

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)

Upvotes: 1

Related Questions