ng-model doesn't work when I create new DOM elements

488 views
Skip to first unread message

Jordi Martínez

unread,
May 11, 2015, 11:29:00 AM5/11/15
to ang...@googlegroups.com

When I create a new DOM element (out of angular), angular doesn't take it under control. Why?

I show you a simplified example :

<html ng-app="gemStore">
  <head>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
    </head>
  <body ng-controller="StoreController as store">
    <button onclick="changeDom();">CreateDomElementWithDataBinding</button>
    <input type='text' ng-model='store.bright'/>
    <div id='placeNewElement'>
    </div>
    <script type="text/javascript">
      angular.module('gemStore',[]).controller('StoreController', function(){
    	  this.name = 'diamont';
    	  this.bright = 3;
      });
      
      var changeDom = function(){
    	  document.getElementById('placeNewElement').innerHTML = "<input type='text' ng-model='store.name'/>";
      };
    </script>
  </body>
</html>

Here, initial DOM is created with an input that have databinding with scope.bright data model. When you click on the button, a new element is created, with ng-model to make a databinding with scope.name. But when new DOM element is created, angular doesn't 'managed' its element (doesn't show scope.name value, doesn't put typical angular classes (ng-valid, etc) to the element).

I searched a lot and I can't solve it :-( I tryed to make scope.apply() to upload model, but this appears not to be the problem (because angular doesn't know this new element).

I can not change the way that new DOM elements are created (is not my hand to change). But I can execute JS after create new elements. Mey be angular just can 'register data binding' on DOM elements when document loads?

Tnaks for help! ;-)

Jordi

Jordi Martínez

unread,
May 11, 2015, 11:33:38 AM5/11/15
to ang...@googlegroups.com
Sorry, I didn't read I can not add code into the mail :-S

Here (http://plnkr.co/edit/nMtLy782IiML91KtAFER?p=preview) you will find the correct way to put code :-)

Kevin Shay

unread,
May 11, 2015, 12:43:31 PM5/11/15
to ang...@googlegroups.com
When adding anything to the DOM that includes bound data, you can use $compile to get Angular to recognize it. Working version:


Cheers,
Kevin

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Jordi Martínez

unread,
May 12, 2015, 6:09:22 AM5/12/15
to ang...@googlegroups.com
Thanks a lot Kevin.

It was what I need :-D

Just I've changed where new DOM element is created (It does another library, I can't change this).

So I pluked code developed from Kevin's answer: http://plnkr.co/edit/PXOtFzEtghw1JjcH0fe2?p=preview

If your see, here I call the 'compile' function by ng-click. This is not good, I've tried to bind with class value (when my body element has a particulary class, angular launch the compile function. But I didn't find how to.

Somebody knows?

Thanks a lot!

Jordi Martínez

unread,
May 12, 2015, 6:13:13 AM5/12/15
to ang...@googlegroups.com
Sorry, I pretend o say that I use onclic event to create the new DOM element (I can not change this :-(, and I use ng-click to call my compile function.

I'd like some 'event listener' triggered when body element have an special class (or something else). Then I'd launch my compile function.

Thanks!
Reply all
Reply to author
Forward
0 new messages