user1650487 user1650487 - 1 year ago 39
AngularJS Question

Angular/Ionic: Can't access value in html input field

I have the following HTML code:

<div class="list">
<label class="item item-input">
<input id="email" ng-model="email" type="text" placeholder="Email Address">
<button class="button button-positive" ng-click="getData();"> Test </button>

And the following Angular code:

$scope.getData = function() {
var emailadd = document.getElementById('email');
var url = ""+emailadd;

However when I try to run the code, the console says that it can't find the variable. Would appreciate guidance here.


Note: Your code in question is wrong anyway as document.getElementById('email') will get you the dom element, this is not the value() of the element.

Still you should be doing this the "Angular Way" :

You need to define the model in your Controller like so :

  $ = {
    text: ''

Then you can bind in the HTML like so :

<input type="email" name="input" ng-model="email.text" required>

Then you can use the $ in your method :

$scope.getData = function() {
   var url = ""+$;

ng-model Documentation