简体   繁体   English

在AngularJS上使用'global'变量

[英]The use of 'global' variables on AngularJS

Ok, I went into a heated argument with a colleague about the way of properly using global variables on AngularJS. 好的,我和一位同事就如何在AngularJS上正确使用全局变量的方式进行了激烈的辩论。 Here's his reasoning: there's many ways to "do" things. 这是他的理由:有很多方法可以“做”事情。

Consider this: 考虑一下:

var app = angular.module('myApp', []);

app.globalVar = []; // this is just to store same data later

Then on some directive/service: 然后在一些指令/服务上:

app.directive('myDirective', function(){
    app.globalVar = [23,43,21]; // populating from a json or other source
});

app.directive('myDirective2', function(){
    var x = app.globalVar;
});

I think this is bad but I would like to know, from a technical view, why this is wrong and what's the proper use of 'global' variables in AngularJS. 我认为这很不好,但是我想从技术角度知道这是错误的原因,以及在AngularJS中正确使用“全局”变量的方法。

I read about using $rootScope and I also read somewhere that on version 2.0 AngularJS is dropping $scope altogheter so maybe the only way will be using a service/factory? 我读到有关使用$ rootScope的信息,并且我还读到某个地方,在2.0版中,AngularJS放弃了$ scope altogheter,所以也许唯一的方法是使用服务/工厂?

In general I do not use global variables. 通常,我不使用全局变量。 They pollute the window scope and can make your code more confusing. 它们污染了窗口范围,会使您的代码更加混乱。 For an example like the one you gave I would recommend using a factory or a service to persist the data. 对于您提供的示例,我建议您使用工厂或服务来保留数据。 Then you can inject it clearly into whatever controller/direct/other-service that you want. 然后,您可以将其清楚地注入到所需的任何控制器/直接/其他服务中。 While your co-worker is correct that there are many ways to do things I do not recommend polluting the global scope. 尽管您的同事是对的,但是有很多方法可以解决问题,但我不建议您污染全局范围。

app.
 module('myApp')
 .factory('myFactory', myFactory);

function myFactory() {
  var globalVar = [23,43,21];

  return {
    get: function() {
      return globalVar;
    }
  }
}

and then in your controller(or whatever) 然后在您的控制器(或其他)中

app.
  module('myApp')
  .controller('MyCtrl', MyCtrl);

  function MyCtrl(myService) {
    var localVar = myService.get();
  }

Below are few conventions I follow while designing 以下是我在设计时遵循的一些约定

a. 一种。 Global Variables - Attach variables to $rootscope and then you can use that variables in any child $scope . 全局变量 -将变量附加到$rootscope ,然后可以在任何子$scope使用该变量。 Moreover, you can even listen to events on rootscope across controllers. 而且,您甚至可以跨控制器在rootscope上监听事件。 Tip : Avoid this because you can end up with too many variables on rootscope and listeners spread across controllers. 提示 :避免这种情况,因为您可能在rootscope上包含太多变量,并且listeners分散在各个控制器上。

b. b。 Angular Service - Use this when you want to share data between two or more controllers. Angular Service-当您要在两个或多个控制器之间共享数据时,请使用此功能。 This is most preferable way to access/share data. 这是访问/共享数据的最佳方法。

c. C。 Angular Factory - Very similar to Service but, use this to store static data such as base urls, api keys, button status between views etc. Angular Factory-与Service非常相似,但是可以使用它来存储静态数据,例如基本url,api键,视图之间的按钮状态等。

d. d。 Angular Controller - Try to keep your controllers with minimal coding and business logic. Angular Controller-尝试使您的控制器保持最少的编码和业务逻辑。 only, routing and basic binding should be done in controllers. 仅路由和基本绑定应在控制器中完成。

I hope it helps in your argument. 希望对您的论点有所帮助。 :) :)

Good way of using Global Variables 使用全局变量的好方法

Angular Service: 角服务:

(function () {

var userService = function () {

    var getGlobalVar = function () {
        var globalVar = [23, 43, 21];
    };
    return {
        globalVar: globalVar
    };
};
var mod = angular.module("myApp");
mod.factory("userService", userService);
}());

Angular Controller: 角度控制器:

app.controller("MyCtrl",['userService' function (userService) {

var test = userService.getGlobalVar();
}]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM