2014-10-02 77 views
1

的Angularjs工廠功能我有一個角模塊/工廠這樣的:如何調用外部

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'); 

當然,這是行不通的,並失敗:

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/......

所以任何人都可以告訴我什麼是p羅伯的方式來調用這個?

+0

是它的工作原理fine.this僅僅是一個非常大的文件我下調和粘貼在這裏的一部分。 – 2014-10-02 13:18:09

+1

或者'angular.injector(['ng','webportal'])。get' – PSL 2014-10-02 13:21:35

+0

這個我比使用document.body更好。 thankyou – 2014-10-02 13:25:03

回答

3

找到了答案周圍多一點戳後:

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