javascript - Angular 形式验证

标签 javascript angularjs validation

我正在将数据作为包含客户端各种详细信息的对象接收到我的 $scope 中。我有以下型号

client.name
client.email
client.programme

在我的函数中,我希望确保每个键都有一个值,并且没有任何未定义的内容。

我认为我可以使用 || 所以遍历每个并检查 if 语句中是否未定义,但这不起作用。

例如:

$scope.addClient = function(newClient) {

console.log(newClient);

  if(!newClient.name ||!newClient.email) {
  $rootScope.$emit('errorEvent',
     {"message" : "You must enter some client name"} 
    );
  };

...performing other functions...

}

其次,我想在错误消息中使用一个变量,以便我可以将其定制为未定义的值。

最佳答案

您没有将 $scope 附加到变量。

用这个

    $scope.addClient = function(newClient) {

    if(!newClient){ 
       $rootScope.$emit('errorEvent',
         {"message" : "You must enter some client name"});
    }

    else if(!$scope.newClient.name || !scope.newClient.email) {
      $rootScope.$emit('errorEvent',{"message" : "You must enter some client name" });
     }
  }

$scope.addClient = function(newClient) {

     if(newClient && (!$scope.newClient.name || !scope.newClient.email) || !newClient ){ 
               $rootScope.$emit('errorEvent',
                 {"message" : "You must enter some client name"});
            }

          }

关于javascript - Angular 形式验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29828818/

相关文章:

javascript - JavsSript RegEx 匹配任何未被标签包围的 ")",即任何不是 ") "的 ">)<"

javascript - ionic 2 本地存储无法将检索到的值设置为变量

html - Struts验证错误后如何清除表格

javascript - 如何检查屏幕高度以进行进一步的 $http 请求?

jquery - 检查输入文件字段是否为空jquery

angular - 如何以 Angular 形式显示服务器验证错误

javascript - React Native 设置另一个组件的状态

javascript - 使用 hashbang 处理多个选项卡

angularjs - 处理 AJAX 请求上的 SAML 重定向

angularjs - 当我们已经在 MEAN Stack 中学习了 Angular 时,是否还需要在 express 中学习 View 和模板引擎