如何在angularjs中重新定义模块?

2024-01-03

我希望我可以在引导后将一个模块附加到主模块。

我发现这个问题:https://github.com/angular/angular.js/issues/3881 https://github.com/angular/angular.js/issues/3881,这正是我想要的。

他们说:

我不再觉得这是必要的,尽管如果我们重新定义模块,看到警告会很高兴(但即使这在测试期间也可能是有益的)

我不知道什么redefine a module意思是,所以我尝试了我的猜测:

html

<div ng-controller="Ctrl">
  {{hi }}
  <input ng-model="hi" />
  <button ng-click="say()">Say</button>
  <ul>
    <li phone="{{p}}" ng-repeat='p in ps'></li>
  </ul>
</div>

你可以看到有一个phone指示。

角码

angular.module('ctrl',[])
.controller('Ctrl', ['$scope', function($scope){
  $scope.hi = 'Hi';
  $scope.say = function() {
    alert("Say something");
  };
  $scope.ps = ["1234567890123","001122334455667"];
}]);


angular.module('app', ['ctrl']);

angular.element(document).ready(function () {
    angular.bootstrap(document, ['app']);
});

你可以看到没有phone定义尚未。

然后我尝试重新定义app模块并附加一个新的phone module:

angular.module('phone', [])
.directive('phone', function() {
  return {
    restrict: "A",
    scope: {
      p : "@phone"
    },
    link: function(scope,el){
      el.text(scope.p.substr(0,5)+"...");
      el.css("cursor", "pointer");
      el.on("click", function(){
        el.text(scope.p);
        el.css("cursor", "text");
      });
    }
  };
});

setTimeout(function() {
    console.log('redefine module');
    angular.module('app', ['ctrl', 'phone']);
}, 3000);

它将在 3 秒内运行。

但这仍然不起作用。我不确定我的方法是否正确,如何解决?

您可以在这里观看现场演示:http://jsbin.com/xukafo/1/edit http://jsbin.com/xukafo/1/edit


Updated:

让我说清楚。我想要附加到主模块的模块是第三方模块,它已经定义了一些模块和指令。由于它又大又慢,我想异步加载它,并将其附加到主(引导)模块。我无法修改该模块的源代码。

已经有一个棘手的解决方案:https://stackoverflow.com/a/18365367/4022015 https://stackoverflow.com/a/18365367/4022015

我已经尝试过该解决方案,它有效,但永远无法通过量角器 e2e 测试。因此,我创建了一个“附加模块”的功能请求,但被告知已经存在一个模块(问题中的那个)。人们说我们不需要这个功能,因为我们可以“重新定义”一个模块,但我不知道如何“重新定义”它。所以就有了这个问题。


重新定义模块意味着,首先定义一个模块:


angular.module('module1', []);  

然后在其他地方再次定义它:


angular.module('module1', [])
    .config(...);  

而目的是添加一个 .config 块(请注意,.module() 是使用单个参数调用的:)


angular.module('module1')
    .config(...);  
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在angularjs中重新定义模块? 的相关文章

随机推荐