Discussion:
[AngularJS] To access ng-model elements using DOM methods [document.getElementById etc]
Pat
2016-03-21 22:41:03 UTC
Permalink
I have defined the following element in angular JS

<input type="text" ng-model="username">

I can access it using $scope.username in controller etc. So far so good.

How can I access it from using DOM model?

var elem = document.getElementById('username'); //does not work
elem.value = "Some value";
console.dir(elem);

var elem = document.getElementById('ng-model="username"'); //does not work
elem.value = "Some value";
console.dir(elem);

This may not be the recommended, but wish to know.

Thanks!
--
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+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.
Sander Elias
2016-03-22 04:34:58 UTC
Permalink
Hi Pat,

You can read from an input element that has an ng-model (well after it is
processed), but setting it will lead to issues. You better do that in your
controllers/components.

var elem=document.querySelector('[ng-model="username"]')
console.log(elem.value)


Regards
Sander
--
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+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.
Pat
2016-03-22 14:48:42 UTC
Permalink
Hi Sander,

Thank you so much!

Pat
Post by Sander Elias
Hi Pat,
You can read from an input element that has an ng-model (well after it is
processed), but setting it will lead to issues. You better do that in your
controllers/components.
var elem=document.querySelector('[ng-model="username"]')
console.log(elem.value)
Regards
Sander
--
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+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.
Loading...