반응형
개체에 속성이 있는지 확인합니다.
객체가 AngularJS에서 특정 속성을 가지고 있는지 확인하려면 어떻게 해야 합니까?
hasOwnProperty'를 사용하여 객체에 특정 속성이 있는지 확인할 수 있습니다.
if($scope.test.hasOwnProperty('bye')){
// do this
}else{
// do this then
}
여기 jsFiddle 데모가 있습니다.
도움이 되길 바랍니다.
if('bye' in $scope.test) {}
else {}
문제는 디렉티브를 링크할 때뿐만 아니라 $http로 로드될 수 있다는 것입니다.
조언은 다음과 같습니다.
controller: function($scope) {
$scope.$watch('test.hello', function(nv){
if (!nv) return;
// nv has the value of test.hello. You can do whatever you want and this code
// would be called each time value of 'hello' change
});
}
또는 값이 1개만 할당되어 있는 것을 알고 있는 경우:
controller: function($scope) {
var removeWatcher = $scope.$watch('test.hello', function(nv){
if (!nv) return;
// nv has the value of test.hello. You can do whatever you want
removeWatcher();
});
}
이 코드는 'test'의 워처 값을 삭제합니다.hello'가 할당되었습니다(컨트롤러, 에이잭스 등).
언급URL : https://stackoverflow.com/questions/20464807/check-if-an-object-has-a-property
반응형
'source' 카테고리의 다른 글
반응 소품:오브젝트 또는 속성을 전달해야 합니까?차이가 많이 나나요? (0) | 2023.02.14 |
---|---|
글로벌 사이트 퍼포먼스를 위해 작은 Ajax 요청이 많거나 큰 요청이 많습니까? (0) | 2023.02.14 |
"현대" 브라우저는 한 번에 몇 개의 HTML 요소를 처리할 수 있습니까? (0) | 2023.02.14 |
프록시를 초기화할 수 없습니다. 세션 없음 (0) | 2023.02.14 |
변경 시 입력 모델이 Integer에서 String으로 변경됨 (0) | 2023.02.14 |