我正在将数据作为包含客户端各种详细信息的对象接收到我的 $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/