简体   繁体   English

Angular / Ionic:无法在html输入字段中访问值

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

I have the following HTML code: 我有以下HTML代码:

        <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: 以下Angular代码:

    $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. 非常感谢这里的指导。

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. 注意:您的代码无论如何都是错误的,因为document.getElementById('email')将获取dom元素,这不是元素的value()

Still you should be doing this the "Angular Way" : 你应该这样做“Angular Way”:

You need to define the model in your Controller like so : 您需要在Controller中定义模型,如下所示:

  $scope.email = {
    text: ''
  };

Then you can bind in the HTML like so : 然后你可以像这样在HTML中绑定:

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

Then you can use the $scope.email in your method : 然后你可以在你的方法中使用$scope.email

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

ng-model Documentation ng-model文档

Use this way of data binding in AngularJs,pass email as parameter in function getData() 在AngularJs中使用这种数据绑定方式,在函数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);
    };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM