我将数据作为一个对象接收到我的$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...
}其次,我想在错误消息中使用一个变量,这样我就可以将其调整为未定义的值。
发布于 2015-04-24 16:27:54
我最终不得不这样做:
if(!client){
$rootScope.$emit('errorEvent',
{"message" : "You must enter a client name"});
}
else if(client.name === undefined) {
$rootScope.$emit('errorEvent',{"message" : "You must enter a client name" });
}
else if(client.email === undefined ) {
$rootScope.$emit('errorEvent',{"message" : "You must enter a client email" });
}
else {
REST OF FUNCTION
}很高兴收到建设性的批评/选择来减少这个代码……
https://stackoverflow.com/questions/29828818
复制相似问题