我有两个属性,我需要为每个属性检查null和undefined,如何在if语句中使用它?
main.js
var validateControlRating = function () {
if ( ($scope.controlProcessRatingDTO.controlPerformanceRatingKey === null ||
$scope.controlProcessRatingDTO.controlPerformanceRatingKey === undefined)
&&
($scope.controlProcessRatingDTO.controlDesignRatingKey === null ||
$scope.controlProcessRatingDTO.controlDesignRatingKey === undefined) ) {
$scope.caculatedRatingDiv = false;
} else {
$http.get('app/control/rest/calculateControlEffectiveness/' + $scope.controlProcessRatingDTO.controlDesignRatingKey + '/' + $scope.controlProcessRatingDTO.controlPerformanceRatingKey).success(function (data) {
$scope.calcaulatedRating = data;
}, function (error) {
$scope.statusClass ='status invalid userErrorInfo';
var errorMessage = error.data.errorMsg;
if (error.data.techErrorMsg) {
errorMessage = error.data.techErrorMsg;
}
$scope.statusInfo = errorMessage;
});
$scope.ratingValidationMsg = '';
$scope.ratingWinValidationClass = 'valid';
$scope.caculatedRatingDiv = true;
$scope.enableRatingSave = false;
}
};
答案 0 :(得分:2)
这在javascript中有点乏味,你必须编写每个条件,并使用括号等
private void button19_Click(object sender, EventArgs e) //Loop button enables timer3 to start
{
timer3.Enabled = true;
timer3.Interval = 401;
timer3.Start();
//sets vehicle mode to max speed
send_ok_flags |= 0x02;
sersendbuf[2] = 0x06;
sersendbuf[3] = 0x13;
sersendbuf[4] = 0x02;
sersendbuf[5] = 0x00;
}
private void button20_Click(object sender, EventArgs e) //stops timer 3
{
timer3.Stop();
timer3.Enabled = false;
}
private void timer3_Tick(object sender, EventArgs e) //timer event
{
Int32.TryParse(maskedTextBox15.Text, out cDest);
if (Math.Abs(cDest - dest) < 300) //close to target destination, go back to starting
{
button1.PerformClick();
counter++;
}
if (Math.Abs(cDest - sDest) < 300) //close to starting destination, go towards target
{
send_ok_flags |= 0x02;
byte[] s1buf = { 0xAB, 0xBA, 0x08, 0x12, 0x00, 0x00, 0x00, 0x00 };
s1buf[4] = (byte)sDest;
sDest >>= 8;
s1buf[5] = (byte)sDest;
sDest >>= 8;
s1buf[6] = (byte)sDest;
sDest >>= 8;
s1buf[7] = (byte)sDest;
s1buf.CopyTo(sersendbuf, 0);
counter++;
}
label46.Text = counter.ToString();
label47.Text = cDest.ToString();
label48.Text = dest.ToString();
}
或只是
if ( ($scope.controlProcessRatingDTO.controlPerformanceRatingKey === null ||
$scope.controlProcessRatingDTO.controlPerformanceRatingKey === undefined)
&&
($scope.controlProcessRatingDTO.controlDesignRatingKey === null ||
$scope.controlProcessRatingDTO.controlDesignRatingKey === undefined) ) {...
答案 1 :(得分:1)
我认为您需要检查此相关内容,检查undefined
然后查看null
并使用&&
而非||
,因为您的代码将检查未定义变量的空值,这肯定会引发异常
的码强>
if( typeof myVar == 'undefined' ? false: myVar )
{ // go here defined and value not null
}
或 的码强>
if(typeof myVar != 'undefined' && myVar)
{ // go here defined and value not null
}
在您的代码中,检查将像
一样if ((typeof $scope.controlProcessRatingDTO.controlDesignRatingKey !== undefined||
typeof $scope.controlProcessRatingDTO.controlPerformanceRatingKey !== undefined) &&
($scope.controlProcessRatingDTO.controlDesignRatingKey !== null ||
$scope.controlProcessRatingDTO.controlPerformanceRatingKey !== null)) {
// do home work
}else { // do other home work }
答案 2 :(得分:0)
你也可以使用否定运算符,但这也会使“假”工作:
if (!$scope.controlProcessRatingDTO.controlPerformanceRatingKey && !$scope.controlProcessRatingDTO.controlDesignRatingKey) {
这有点短,但如果你想分别处理False值,那么请使用上面的Adeneo答案。
答案 3 :(得分:0)
你可以这样做:
if ( some_variable == null ){
// some_variable is either null or undefined
}
取自:How to check for an undefined or null variable in JavaScript?