Is there an angular equivalent to the typeof operator in JavaScript that can detect variables not defined? I am specifically interested in the behavior of angular.isDefined() and how it differs from typeof. In the example below, the variable x
is not Defined, however, using typeof x
does not throw any errors. On the other hand, if we had used angular.isDefined
, a "not defined error" would have been thrown.
if(typeof x != 'undefined')
alert("defined");
else
alert("undefined");
When using angular.isDefined(x), a
Uncaught ReferenceError: x is not defined
error is thrown if x is not Defined.
if(angular.isDefined(x)) // throws error because x is not defined
alert("defined");
else
alert("undefined");
var x;
if(angular.isDefined(x)) // does not throw error as x is defined though not **initialiased**
alert("defined");
else
alert("undefined");
Is there an Angular solution similar to typeof x != 'undefined'
? I am facing this situation where I cannot modify the createContext()
function which defines context = "something useful"
. There are two controllers involved - commonController
and contextController
. The commonController registers for a custom event handler that requires access to context
, but sometimes this event handler is triggered before createContext()
is called. Moving createContext()
outside of contextController
is not an option. How can I handle this scenario in Angular?