AngularJs(Part 1)

I am tired to translate these into Chinese.

but who cares? i write these posts just for myself


Scope

a new scope is created by the ng-controller directive using
the Scope.$new() method call.
yes , we need to have at least one instance of a scope before
creating a new scope!
so here is the $rootScope. it is the paremt of all the other
scopes. the $rootScope instance gets created when a new
application is bootstrapped.
each scope contains a property $parent which points to its parent scope.

ng-controller is a scope-created directive.
there are many other scope-created directives.

Scopes form a parent-child ,tree-like relationship rooted at
the $rootScope instance. and scopes' creation is driven by the DOM tree.

check the famous ng-repeat.
let's say a HTML snippet<li ng-repeat='per in persons'>{{per.name}}</li>
when interating persons, a new variable needs to be created to hold each per.
it seems the Google does not use the strategy that just simplely override previous value.
instead , it creats a new scope for each per and expose the scope to maybe the parent scope $scope.
in this way, it's possible to create variable with the same name on different scopes without createing name collisions.
in above snippet, every <li> element gets its own scope when the per variable can be defined.


as i say above ,each $scope except the $rootScope contains a property $parent which pointing to parent scope,
so all properties defined in parent scope is actually available to child scope.


Scope's inheritance in AngularJs follws the same rules as prototypical inheritance in JavaScript.(when we try to read
as property, the inheritance tree will be traversed upwards till a property is found.)

Scopes are responsible to creat variables.
they are to provide isolated namespaces and avoid name collisions.
they can be destroyed and the garbage collection will deallocate the resource.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值