In addition, is there a js similar to main.js in which global variables can be defined? For example, var http = "www.xxxx.com/"
can make other Ctrl.js callable.
高洛峰2017-05-15 16:55:43
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
PHPz2017-05-15 16:55:43
$rootScope is an object and can be bound to global variables in the form of $rootScope.xxx='';
某草草2017-05-15 16:55:43
@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
我想大声告诉你2017-05-15 16:55:43
angular.module('app', []).run(function($rootScope) {
$rootScope.http = 'www.xxxx.com/';
});