The idea is wrong, angular中不需要定义全局变量。你的数据应该通过service, factory, providerwill provide it.
And these things can be dependency injected, so there is no need for global variables at all
@leftstick is right, AngularJS uses dependency injection style to structure the entire application framework. It is best not to declare global variables, but to make it a Service. This way you can avoid a lot of problems:
Naming conflict. Naming conflicts in JavaScript will not give any warning. If your project is small, the probability of occurrence is small, but it is very difficult to debug.
Initialization sequence. If you get execution before ACtrl中定义了一个全局变量window.a,比如你想在BCtrl中使用它,那么你需要保证ACtrl在BCtrl, this is often difficult to implement and even logically confusing.
In Angular, the most reasonable way is to put a做成一个aService,注入到ACtrl和BCtrl inside.
How to define a service and the relationship between Module, Service, Factory and Provider can refer to this blog:
The idea is wrong,
angular
中不需要定义全局变量。你的数据应该通过service
,factory
,provider
will provide it.And these things can be dependency injected, so there is no need for global variables at all
$rootScope is an object and can be bound to global variables in the form of $rootScope.xxx='';
@leftstick is right, AngularJS uses dependency injection style to structure the entire application framework. It is best not to declare global variables, but to make it a Service. This way you can avoid a lot of problems:
ACtrl
中定义了一个全局变量window.a
,比如你想在BCtrl
中使用它,那么你需要保证ACtrl
在BCtrl
, this is often difficult to implement and even logically confusing.In Angular, the most reasonable way is to put
a
做成一个aService
,注入到ACtrl
和BCtrl
inside.How to define a service and the relationship between Module, Service, Factory and Provider can refer to this blog:
http://harttle.github.io/2015/06/07/angular-module.html
Now angularjs2 is out