source

null, 비어 있거나 정의되지 않은 angularjs 확인

itover 2023. 2. 15. 21:51
반응형

null, 비어 있거나 정의되지 않은 angularjs 확인

angularjs를 사용하여 프로젝트를 만들고 있습니다.나는 다음과 같은 변수를 가지고 있다.

$scope.test = null
$scope.test = undefined
$scope.test = ""

모든 null, undefined 및 empty 값을 하나의 조건으로 확인하고 싶다.

그냥 사용 -

if(!a) // if a is negative,undefined,null,empty value then...
{
    // do whatever
}
else {
    // do whatever
}

이는 javascript의 ===와 ==의 차이 때문에 작동하는데, 이는 일부 값을 다른 유형의 "최소" 값으로 변환하여 동일성을 확인하는 ===와는 반대로, 기본적으로 === 운영자는 값이 동일한지 여부를 확인하는 "", null을 잘못된 값으로 변환하는 방법을 알고 있습니다.그게 바로 네게 필요한 거야

할수있습니다

if($scope.test == null || $scope.test === ""){
  // null == undefined
}

한다면false,0그리고.NaN잘못된 값으로 간주될 수도 있습니다.

if($scope.test){
 //not any of the above
}
if($scope.test == null || $scope.test == undefined || $scope.test == "" ||    $scope.test.lenght == 0){

console.log("test is not defined");
}
else{
console.log("test is defined ",$scope.test); 
}

앵귤러 함수를 사용하면angular.isUndefined(value)부울을 반환합니다.

Angular의 함수에 대한 자세한 내용은 여기를 참조하십시오. AngularJS 함수(정의되지 않음)

간단한 체크가 가능합니다.

if(!a) {
   // do something when `a` is not undefined, null, ''.
}

매우 간단한 체크:

설명 1:

if (value) {
 // it will come inside
 // If value is either undefined, null or ''(empty string)
}

설명 2:

(!value) ? "Case 1" : "Case 2"

값이 정의되지 않은 경우, null 또는 "일 경우 Case 1 이외의 값 Case 2에 대해 Case 1이 정의되지 않은 경우.

기능을 사용하여 간단한 체크도 할 수 있습니다.

$scope.isNullOrEmptyOrUndefined = function (value) {
    return !value;
}

언급URL : https://stackoverflow.com/questions/36124986/check-null-empty-or-undefined-angularjs

반응형