无法通过适当的值传递表单验证

时间:2016-06-09 05:07:06

标签: angularjs angular-directive

我已经制定了一个使用表单验证的指令,如果移动设备尚未在服务中存在,但它显示每10位数的验证,不管它是否存在。请告诉我哪里出错了 这是我的代码。

HTML

 <div class="col-xs-8">
                        <md-input-container>
                            <md-icon md-svg-icon="assets/images/device.svg"></md-icon>
                            <input type="number" placeholder="Enter user mobile no" name="mobile" id="mobile" ng-model="mobile" required minlength="10"  ng-pattern="/^[789]\d{9}$/" phone-number-validator>
                                <div ng-messages="myForm.mobile.$error">
                                <div ng-message="required">Registered mobile no.</div>
                                    <div ng-message="phoneNumber">The mobile No entered has already been registered</div>
                                <div ng-message="test">too short</div>
                            </div>
                        </md-input-container>
                    </div>

这是我的指示

angular.module('shoppingPad').directive('phoneNumberValidator',phoneNumberValidator)


function phoneNumberValidator($q,loginServices){
    //var deferred=$q.defer();
    return{
        restrict:'EA',
        require:'ngModel',

        link:function($scope,element,attrs,ngModel){
            ngModel.$asyncValidators.phoneNumber=function(modelValue , viewValue) {

                var mobile= (modelValue).toString();
                console.log(mobile);
                console.log("abc");

                    if(mobile.length == 10){

                        return $q(function(resolve,reject){
                            if(mobile==loginServices.getMobileNo.mobile1){
                                resolve();

                            }
                            else reject();
                        });

                }

            }
        }

这是我的服务,从中传递数据

this.getMobileNo=function(){
        var mobile={
            mobile1:'8898246388',
            mobile2:'8898302454',
            mobile3:'8898454302'
        }
}

1 个答案:

答案 0 :(得分:1)

你的代码有两个错误: 此方法getMobileNo必须返回对象

this.getMobileNo=function(){
    return mobile={
        mobile1:'8898246388',
        mobile2:'8898302454',
        mobile3:'8898454302'
    }
 }

然后在致电您的服务时,需要()来调用一个函数: if(mobile==loginServices.getMobileNo().mobile1){