1

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

asked Oct 18, 2016 at 5:14
2
  • 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. Commented Oct 18, 2016 at 5:15
  • @GünterZöchbauer thnx for reply, I have update question Commented Oct 18, 2016 at 5:29

1 Answer 1

1

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)

answered Oct 18, 2016 at 5:31
Sign up to request clarification or add additional context in comments.

7 Comments

So whatever javascript API I am using which returns the DOM objects, it won't work for binding?
Exactly. You can't add/remove binding dynamically. A workaround could be to create components at runtime and add such a component. Components created at runtime can introduce new bindings. See for example stackoverflow.com/questions/40060498/…
One more question, Can I get the DOM object reference from code example which you have shared?
Sorry, I don't understand what "DOM object reference" you mean.
example which you have shared, Inside that there is variable called html, I guise which takes the html string element, right?. Now I want the DOM object of this html string
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.