AngularJS 中的 $scope 与 var [英] $scope vs var in AngularJS

查看:22
本文介绍了AngularJS 中的 $scope 与 var的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 var 和 $scope.varname 并且在 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?

推荐答案

是的!$scope 变量绑定到视图,而 var 没有绑定到视图,并且对于它声明的函数是本地的!

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

这篇关于AngularJS 中的 $scope 与 var的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆