如何调用来自外部的Angularjs工厂函数 [英] How to call an Angularjs factory function from outside

查看:187
本文介绍了如何调用来自外部的Angularjs工厂函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个角模块/工厂是这样的:

I have an angular module / factory like this:

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 从外部的角度,像这样的:

Now i am trying to call the getURLParameter from outside angular, like this:

angular.injector(['webportal']).get('portal').getURLParameter('blah');

当然,这并不工作,无法与

Of course that does not work and fails with:

错误:[$喷油器:modulerr]未能实例由于模块门户网站
  到:[$喷油器:modulerr]无法实例模块
  vr.directives.slider由于:未能:[$注射器modulerr]
  实例化模块ngTouch由于:[$喷油器:unpr]未知提供商:
  ngClickDirectiveProvider
   http://errors.angularjs.org/1.2.25/ $喷油器/ unpr?P0 = ngClickDirectiveProvider
  minErr /......

Error: [$injector:modulerr] Failed to instantiate module webportal due to: [$injector:modulerr] Failed to instantiate module vr.directives.slider due to: [$injector:modulerr] Failed to instantiate module ngTouch due to: [$injector:unpr] Unknown provider: ngClickDirectiveProvider http://errors.angularjs.org/1.2.25/$injector/unpr?p0=ngClickDirectiveProvider minErr/......

因此​​,谁能告诉我什么是调用正确的方法?

So can anyone tell me what is the proper way to call this?

推荐答案

找到了答案周围多一点戳后:

found the answer after poking around a bit more:

angular.element(document.body).injector().get('portal').getURLParameter('blaha')

这篇关于如何调用来自外部的Angularjs工厂函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆