itamar itamar - 1 year ago 95
AngularJS Question

$scope vs var in AngularJS

I've been using var and $scope.varname and both work fine in AngularJS. I was wondering if there was a difference between the two functionally, and what best practice was in which one to use?

Answer Source

Yes! $scope variables bind to the view where as var does not and is local to the function it was declared in!

var x = 3;
$scope.y = 3;

{{x}} - undefined
{{y}} - 3
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download