我可以在运行时获得所有已注册模块的列表吗?
例如:
// Some code somewhere in some .js file
var module1 = angular.module('module1', []);
// Some code in some other .js file
var module2 = angular.module('module2', []);
// Main .js file
var arrayWithNamesOfAllRegisteredModules = .....
// (result would be: ['module1', 'module2'])
Run Code Online (Sandbox Code Playgroud) 有没有办法列出为给定角度模块定义的所有指令和控制器?例如,假设我在'main'模块中定义了三个控制器(即angular.module('main').controller('MainCtrl',function(){...}).是否有办法获取列表那三个控制器?
我想$templateCache通过从devtool的控制台访问它来检查内容.
我尝试了以下解决方案:https://stackoverflow.com/a/24711132.
这不适合我.
我怎样才能做到这一点?
我有几个服务使用Web服务并缓存大量结果.通过缓存我的意思是存储在服务上的变量.当用户注销时,应清除数据.服务如下所示(简化版):
class DataService {
private data;
constructor($http)
{
$http.get(url).then((response) =>
{
this.data = response.data;
});
}
Run Code Online (Sandbox Code Playgroud)
}
哪个是打字稿,但解析成这样的东西:
var DataService = (function () {
function DataService($http) {
var _this = this;
$http.get(url).then(function (response) {
_this.data = response.data;
});
}
return DataService;
})();
Run Code Online (Sandbox Code Playgroud)
$rootScope.on('logout',function(){
this.data = [];
});
Run Code Online (Sandbox Code Playgroud)
但是,当我们有多个服务和控制器时,这是很多代码.我们都知道这个新人会在服务中添加一些新数据,并忘记将其添加到注销序列中.这简直是一种不好的做法.
类似地,数据存储在应用程序各个部分的$ scope中,这也必须清除.范围相当简单,因为控制器的构造函数在每次页面访问时加载,然后将覆盖数据.
一个提议的解决方案是进行刷新,但这会给用户带来可怕的体验.
一种解决方案可能是使角度相信从未创建过服务或完全重新加载角度.
做这个的最好方式是什么?将数据存储在服务变量中是不是很糟糕?
是否有可能获得我已定义的所有Angular控制器的列表?基本上我希望能够确定files我需要导入哪些(我写的)取决于使用哪些.我能想到的唯一方法是遍历HTML并找到与之关联的所有值ng-controller,但我想知道是否有更清晰,更健壮的方式.
我想知道是否有办法将我在AngularJS模块中定义的所有工厂导入控制器而无需全部列出.假设我有一个名为contains的文件foo.js:
angular.module("Foo", [])
.factory("Bar1", function() {...})
.factory("Bar2", function() {...})
.factory("Bar3", function() {...})
.factory("Bar4", function() {...});
Run Code Online (Sandbox Code Playgroud)
现在,在我的controller.js档案中,我有:
angular.module("myApp.controllers", ["Foo"]).
controller("MainCtrl", ["Bar1", "Bar2", "Bar3", "Bar4", function(bar1, bar2, bar3, bar4) {
//do stuff with the various bars
}]);
Run Code Online (Sandbox Code Playgroud)
我只是想知道控制器是否有任何优雅的方式,因为它已经导入模块Foo,看到它的所有工厂(或提供者,或服务,或指令).