javascript – 使用AngularJS共享模块?

前端之家收集整理的这篇文章主要介绍了javascript – 使用AngularJS共享模块?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
假设我有一个我想要注入到myApp配置的东西模块:
  1. angular.module('myApp',['stuff']).
  2. config([function() {
  3.  
  4. }]);

有两个子模块:

  1. angular.module("stuff",["stuff.thing1","stuff.thing2"]);

这是第一个:

  1. angular.module('stuff.thing1',[]).provider("$thing1",function(){
  2. var globalOptions = {};
  3. this.options = function(value){
  4. globalOptions = value;
  5. };
  6. this.$get = ['$http',function ($http) {
  7. function Thing1(opts) {
  8. var self = this,options = this.options = angular.extend({},globalOptions,opts);
  9. }
  10. Thing1.prototype.getOptions = function(){
  11. console.log(this.options.apiKey);
  12. };
  13. return {
  14. thing1: function(opts){
  15. return new Thing1(opts);
  16. }
  17. };
  18. }];
  19. });

第二个例子是相同的:

  1. angular.module('stuff.thing2',[]).provider("$thing2",function ($http) {
  2. function Thing2(opts) {
  3. var self = this,opts);
  4. }
  5. Thing2.prototype.getOptions = function(){
  6. console.log(this.options.apiKey);
  7. };
  8. return {
  9. thing2: function(opts){
  10. return new Thing2(opts);
  11. }
  12. };
  13. }];
  14. });

您会注意到,您可以访问它们作为提供程序来配置选项:

  1. angular.module('myApp',['stuff']).
  2. config(['$thing1Provider','$thing2Provider',function($thing1Provider,$thing2Provider) {
  3. $thing1Provider.options({apiKey:'01234569abcdef'});
  4. $thing2Provider.options({apiKey:'01234569abcdef'});
  5. }]);

如果我们在控制器中,您可以覆盖每个范围,如:

  1. controller('AppController',['$scope','$thing1',function($scope,$thing1) {
  2. var thing1 = $thing1.thing1({apiKey:'3kcd894g6nslx83n11246'});
  3. }]).

但是如果他们总是分享相同的财产呢?如何在供应商之间分享内容

  1. angular.module('myApp',['stuff']).config(['$stuff' function($stuff) {
  2. //No idea what I'm doing here,just trying to paint a picture.
  3. $stuff.options({apiKey:'01234569abcdef'});
  4. }]);

可以为$thing1和$thing2注入$东西并配置共享属性吗?

如何访问$thing1和$thing2作为单个模块的扩展?

  1. controller('AppController','$stuff',$stuff) {
  2. //Again - no idea what I'm doing here,just trying to paint a picture.
  3.  
  4. //$thing1 would now be overwrite $stuff.options config above.
  5. var thing1 = $stuff.$thing1.thing1({apiKey:'lkjn1324123l4kjn1dddd'});
  6.  
  7. //No need to overwrite $stuff.options,will use whatever was configured above.
  8. var thing2 = $stuff.$thing2.thing2();
  9.  
  10. //Could I even change the default again for both if I wanted too?
  11. $stuff.options({apiKey:'uih2iu582b3idt31d2'});
  12. }]).

解决方法

将模块注入两个共享这些属性的模块.

使用provider类覆盖属性或将其从任何范围中实例化:

  1. angular.module("stuff.things",[]).provider("$things",function(){
  2. var globalOptions = {};
  3. this.options = function(value){
  4. globalOptions = value;
  5. };
  6. this.$get = [,function () {
  7. function Things(opts) {
  8. var self = this,opts);
  9. }
  10. Things.prototype.returnOptions = function(){
  11. return this.options;
  12. };
  13. return {
  14. things: function(opts){
  15. return new Things(opts);
  16. }
  17. };
  18. }];
  19. });

秘密酱:$things.things().returnOptions()

  1. angular.module('stuff.thing1',['stuff.things']).provider("$thing1",function(){
  2. var globalOptions = {};
  3. this.options = function(value){
  4. globalOptions = value;
  5. };
  6.  
  7. this.$get = ['$things',function ($things) {
  8. function Thing1(opts) {
  9. var self = this,$things.things().returnOptions(),opts);
  10. ...
  11. }
  12. return {
  13. thing1: function(opts){
  14. return new Thing1(opts);
  15. }
  16. };
  17. }];
  18. });

猜你在找的JavaScript相关文章