ng-model值在控制器中未定义

时间:2015-12-09 20:07:13

标签: javascript angularjs ionic-framework ionic

我试图制作一个视图,只需通过ng-model接收电子邮件和密码,然后通过ng-click将其传递给控制器​​中的登录功能。我似乎无法让它正常工作,过去有关范围继承和$ parent的帖子似乎没有为我解决问题。

ng-model属于离子含量和标签,这就是我的假设导致范围继承问题的原因。此外,视图{{user.email}}中发生的数据绑定永远不会更新。

这是一个我试图重新创建问题的plunker:http://plnkr.co/edit/JFlO64xi4nG84xqFDYsv?p=info

HTML

<body ng-controller='MyCtrl'>

    <ion-content>
    <div class="list">
      <label class="item item-input">
        <input type="email" placeholder="Enter Email" ng-model="user.email">
      </label>
      <label class="item item-input">
        <input type="email" placeholder="Enter Password" ng-model="user.password">
      </label>
      {{user.email}}
      {{user.password}}
      <button class="button button-block button-positive" ng-click="login(user.email,user.password)">Log in</button>
    </div>
  </ion-content>

  </body>

的JavaScript

angular.module('starter', ['ionic'])

.controller('MyCtrl', function($scope) {
  $scope.user = {
    email: '',
    password: ''
  }
  $scope.login = function(email,password) {
    console.log("email is: ",email); // will log undefined
    console.log("password is",password); // will log undefined
  };
})

我试过$ parent.user.email,但这也行不通。有人可以帮我弄清楚这里出了什么问题吗?

4 个答案:

答案 0 :(得分:4)

您已设置type="email",这将导致验证发生。当字段无效时,不会更新基础模型。将input更改为type="text"以查看差异。

有关详细信息,请参阅input[email]上的the docs

答案 1 :(得分:0)

需要进行更改。

int main(int argc, char *argv[])
{
    switch (fork())
    {
    case -1:
        perror("fork");
        return 1;
    case 0: // child
        sleep(seconds(5));
        printf("%07u %s: Process group ID = %d\n", getpid(), currTime("%T").c_str(), getpgrp());
        printf("%07u %s: Foreground process group ID = %d\n", getpid(), currTime("%T").c_str(), tcgetpgrp(STDIN_FILENO));
        break;
    default: // parent
        printf("%07u %s: Process group ID = %d\n", getpid(), currTime("%T").c_str(), getpgrp());
        printf("%07u %s: Foreground process group ID = %d\n", getpid(), currTime("%T").c_str(), tcgetpgrp(STDIN_FILENO));
        break;
    }

    return 0;
}

测试时,请不要忘记输入电子邮件地址。好的验证是另一个问题

答案 2 :(得分:0)

显然这是一个众所周知的问题 - &gt;的 https://github.com/driftyco/ionic/issues/555

解决方案:只需将离子升级到1.1.1。

正如@stevuu指出的那样,您应该/也可以将输入type设置为text - 在email类型上,内容将被验证并返回undefined user.email不是有效的电子邮件。

当进行这两项更改时,它可以正常工作。

forked plnkr - &gt;的 http://plnkr.co/edit/yPiFQkzfNJFDr1gOWFry?p=preview

如果电子邮件无效,您还可以利用type="email"并显示错误消息:

<input type="email" placeholder="Enter Email" ng-model="user.email">
...
{{ error }}
$scope.login = function(email,password) {
  $scope.error = !email ? 'Email is not valid' : '';
  console.log("email is: ",email); 
  console.log("password is",password); 
};

<强> http://plnkr.co/edit/pOl88KdOno0keQwePyvH?p=preview

答案 3 :(得分:-1)

在HTML中,尝试将类型更改为文本,并在输入中添加“name”字段,例如:

<input name="email" type="text" placeholder="Enter Email" ng-model="user.email">

名称必须与对象(模型)属性相同,在您的情况下,此处对象(模型)用户的属性为“email”。 所以其他输入应该如下所示:

<input name="password" type="text" placeholder="Enter Password" ng-model="user.password">

此处对象(模型)用户的属性是密码,因此名称也是“密码”。

希望有所帮助!