1

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">
            </label>
            <br> 
            <center>
            <button class="button button-positive" ng-click="getData();"> Test </button>
             </center>
        </div>

And the following Angular code:

    $scope.getData = function() {
        var emailadd = document.getElementById('email');
        var url = "http://172.16.99.138/sendemail.php?emailaddress="+emailadd;
        console.log(url);
    };

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

3
  • Can you elaborate please Commented Jun 9, 2016 at 15:23
  • Don't use the dom elements for bindings like this, that's the main point of using angular, 2-way-binding.. Commented Jun 9, 2016 at 15:25
  • Still a bit confused as to how I should solve this. New to angular here... Commented Jun 9, 2016 at 15:26

2 Answers 2

2

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 :

  $scope.email = {
    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 $scope.email in your method :

$scope.getData = function() {
   var url = "http://172.16.99.138/sendemail.php?emailaddress="+$scope.email.text;
   console.log(url);
};

ng-model Documentation

Sign up to request clarification or add additional context in comments.

6 Comments

It is not necessary i think, to create object only to catch that input value.Angular it is more useful and beautiful :)
@FationHadri assume the user would like to store the observable elsewhere, or infact he has complex objects, or he has resolved the value elsewhere in a service, he must then manage more on the front-view end. It's usualy good practice to maintain less in the view. But your answer works perfectly too for the OP's issue.
@Pogrindis, Yes, i agree with you,but here we have a simple situation and it doesn't make sense to me that object created in controller.I think it is not right.He don't want to have a static email.It is input.You do not limit the user what he should write in email.Sorry but i think you have made a big mistake here.
@FationHadri big mistake would be a little over-exaggerated, but I have removed the static email for brevity. -> To help explain - in your example, how would you populate the fields from the database on arrival for example ? -> If you do not have #scope.email defined in controller.. It will not be too useful.
@Pogrindis, If we disscuss on what he might have to do,we ,will find a lot.I specified" big mistake" based on your reputation :)
|
1

Use this way of data binding in AngularJs,pass email as parameter in function getData()

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

And in your controller

  $scope.getData = function(email) {
       // var emailadd = document.getElementById('email');don't use this
        var url = "http://172.16.99.138/sendemail.php?emailaddress="+email;
        console.log(url);
    };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.