gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 22:27:18 25 4
gpt4 key购买 nike

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

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

他们说:

I no longer feel this is necessary, although it would be nice to see warnings if we redefine a module (but even this may be beneficial during testing)

我不确定重新定义模块是什么意思,所以我尝试了我的猜测:

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 代码

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 模块:

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

<小时/>

更新:

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

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

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

最佳答案

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

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

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

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

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

angular.module('module1')    .config(...);

关于angularjs - 如何在angularjs中重新定义模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25742259/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com