threxx threxx - 2 years ago 63
AngularJS Question

Manually add scope to html element via JS Ctrl

I want to create a new element in my controller and then add a scope variable to this. The way I did it, I only see '{{text}}' in the view instead of the variable value of $scope.text.

That's my code so far:


var elem = document.getElementById('match-text');
var newElem = document.createElement('span');
newElem.setAttribute('id', 'inner-match-text');
newElem.setAttribute('class', 'ng-binding');
newElem.innerText = '{{text}}';


<h3 id="match-text"></h3>

Maybe this is the totally wrong approach, I don't know. Can someone tell me how to do this properly?

Answer Source

Is it necessary to use quotes?

newElem.innerText = $scope.text;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download