4

I am confuse about how angular scope works!!!

I am passing two variable to one template using ng-init and onLoad. Both variable value accessed from template but there is one controller which I have bound to that template. I am thinking why both variable prints its value from template but prints undefined (onLoad varibale) while accessing from controller.

Fiddle illustrating my problem jsFiddle

What I understand is, when we pass variable using ng-init, it creates that variable inside current scope but what for that variable which is passed by onLoad??

Please tell me how it exactly works.

Thank you.

4

2 回答 2

3

第二个控制器可以访问这两个变量。

但是(看起来)第二个控制器是在计算onload表达式之前创建的。话虽如此,如果您想访问范围变量,您可以通过手表来访问(无论如何,您可能希望使用手表而不是使用范围变量的初始值 - 如果您的变量是承诺的结果呢? ?)。

这是一个示例,显示您可以从第二个控制器访问负载:

http://jsfiddle.net/rDP2t/3/

于 2013-08-26T11:28:12.203 回答
2

因为在浏览器解析页面之前,ng-init 是由 angular 评估的。所以控制器选择第一个值。在此之后发生加载。因此在 onload 时设置该值为时已晚。

于 2013-08-26T11:19:52.943 回答