$scope vs var in AngularJS

后端 未结 5 1838
暖寄归人
暖寄归人 2020-12-05 17:38

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 i

5条回答
  •  北海茫月
    2020-12-05 17:55

    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
    

提交回复
热议问题