Why can't I do this in Angular.js:
document.getElementById('divid').value = 'Change Text to This';
And what is the "right" (Angular) way of doing it?
Why can't I do this in Angular.js:
document.getElementById('divid').value = 'Change Text to This';
And what is the "right" (Angular) way of doing it?
You'd have to bind a controller to your mark up and initialise your Angular app.
Then you can simply define a scope variable in your controller
It is considered best to use the ng-app directive in the HTML tag of the page
jsFiddle
In Angular you use the
ng-model
directive in order to create a two-way data binding between your model (i.e. a JS variable) and the view (i.e. the<div>
). Basically, this means that whenever you change the data in the view (through an input, for instance), the change will be reflected in your JS variable. See below:HTML:
JS:
Here, myVariable will have "initialValue" as an initial value. Any further changes to the input will overwrite the value of this variable.
Also notice that
{{myVariable}}
injects the variable the data into your div. When you change the value in the input, the div text will be changed as well.