Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Is there a way to access a global JavaScript variable in a Angular expression? such as:

ng-disabled="{someJavascriptVariableHere}"
share|improve this question

1 Answer 1

up vote 1 down vote accepted

Angular expressions will look only for variables on the $scope, but if you do something like this in your controller:

// assumes someJavascriptVariableHere exists in the global scope
$scope.someJavascriptVariableHere = someJavascriptVariableHere;

then you can access it.

share|improve this answer
1  
This answer is correct, but I wanted to add that the reason you can access a global variable directly inside of an angular expression is because the angular expression is being evaluated in the context of the scope and if the scope or it's parent scopes don't know the global variable then it will implicitly create the variable. –  James Aug 2 '13 at 22:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.