ng-model 无法从特定方法的输入中获取值



我想使用ng-keypress检查电子邮件重复。因此,我宣布NG模型" USREMAIL"。当它在"寄存器"方法中使用时,它可以用输入值绑定,但不能在" checkDuplication"中使用输入值绑定。

如何解决此问题??

Ingip.html

<input type="email" class="form-control" name="usrEmail" ng-model="usrEmail" ng-required="true" ng-keyup="checkDuplication()">
...
<button class="btn btn-primary full-width" ng-click="register()">Register</button>

Ingip.js

var methods = {
    checkDuplication: function($scope) {
        $scope.usrEmail;
        console.log($scope.usrEmail); // undifined!!!!!!!
        ...
    },
    register: function($scope){
        $scope.user.usrEmail =  $scope.usrEmail; // successfully binded!
        ...
    }

谢谢!

您不应将$ scope作为参数传递给singup.js(控制器/服务(。您应该像ng-keyup="checkDuplication(usrEmail)"这样的标记中传递模型,并在功能中使用:

checkDuplication: function(usrEmail) {
    console.log(usrEmail);
    ...
}

这是一个可以帮助的plunker:https://plnkr.co/edit/bl4ui3lbdckvrg8xdjxp?p=peview

尝试此

<input type="email" class="form-control" name="usr-Email" ng-model="usrEmail" ng-required="true" ng-keyup="checkDuplication()">

最新更新