如何一起声明两个ng-change功能

时间:2017-02-21 02:28:18

标签: javascript angularjs

我想问一下,我正在尝试一起实现phonechange和emailchange功能。我试图调用该函数,如果phonechange函数和emailchange函数为true,它将提醒我您输入的电话号码和电子邮件是有效的。非常感谢你的帮助

$scope.phone = {data: ""};
$scope.email = {data: ""};
$scope.FirstName = {data: ""};
$scope.LastName = {data: ""};



$scope.phonechange = function (){
  phone = this.phone.data;
if(PHONE_REGEXP.test(this.phone.data)){
  alert('phone true');

  $scope.invalid = false;
}else{
  console.log('false');
    $scope.invalid = true;

}

}

$scope.emailchange = function(){
  if(re.test(this.email.data)){
  alert('email true');
    $scope.invalid = false;

}else{
  console.log('false');
    $scope.invalid = true;

}
}

1 个答案:

答案 0 :(得分:2)

为每个输入添加一个函数:

<input name="phone" ng-model="phone" ng-change="phoneChange()"/>
<input name="email" ng-model="email" ng-change="emailChange()"/>

控制器(只是建议你):

$scope.phone = '';
$scope.email = '';
$scope.firstName = '';
$scope.lastName = '';

$scope.isvalid = {
  phone: true,
  email: true
};


$scope.phonechange = function (){

  $scope.isvalid.phone = PHONE_REGEXP.test($scope.phone) // true|false;

  if ($scope.isvalid.phone) {
    // Do something
  }

};

$scope.emailchange = function(){
  $scope.isvalid.email = re.test($scope.email);

  if ($scope.isvalid.email) {
    // Do something
  }
};

但是,您可以使用ng-pattern进行验证。查看更多here