F
F
Forge01002016-04-14 22:56:32
Angular
Forge0100, 2016-04-14 22:56:32

How to check if $scope exists in AngularJS?

When the variable is empty, I cannot catch the error so that I can call the desired function in the else .

if(typeof $scope.formData.telephone == undefined) {
   //...
} else {
   //...
}

Error in console
TypeError: Cannot read property 'telephone' of undefined

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2016-04-15
@Forge0100

if($scope.formData && $scope.formData.telephone) {
ps Stop using $scope!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question