我有一个像这样的角度模块/工厂:
var app = angular.module('webportal', ['vr.directives.slider', 'angular-flexslider', 'LocalStorageModule', 'multi-select']);
app.factory('portal', ['uri', function (uri) {
portal = {};
portal.getURLParameter = function (name) {
var hash;
var vars = [];
var indexOfQuestion = window.location.href.indexOf('?');
if (indexOfQuestion != -1) {
var hashes = window.location.href.slice(indexOfQuestion + 1).split('&');
for (var i = 0; i < hashes.length; i++) {
if (typeof name == 'undefined')
vars.push(hashes[i]);
else {
hash = hashes[i].split('=');
if (hash[0] = name)
return hash[1];
}
}
}
};
return portal;
}]);
现在我试图从外角调用getURLParameter
,如下所示:
angular.injector(['webportal']).get('portal').getURLParameter('blah');
当然,这不起作用,但失败了:
错误:[$ injector:modulerr]无法实例化模块webportal到期 to:[$ injector:modulerr]无法实例化模块 vr.directives.slider由于:[$ injector:modulerr]失败 实例化模块ngTouch由于:[$ injector:unpr]未知提供者: ngClickDirectiveProvider http://errors.angularjs.org/1.2.25/ $注射器/ unpr?P0 = ngClickDirectiveProvider minErr /......
所以有人能告诉我这是什么方法吗?
答案 0 :(得分:5)
找到了答案之后再说一遍:
angular.element(document.body).injector().get('portal').getURLParameter('blaha')