2016-06-10 26 views
1

habe ich individuelle ServiceAufruf öffentliche Methode von einer anderen js Funktion in Service

(function() { 
    "use strict"; 

    angular 
     .module("common.services")   
     .factory("redirectService", 
       ["$q", "$location", 
       redirectService]) 
     .config(function ($httpProvider) { 
      $httpProvider.interceptors.push('redirectService'); 
     }); 

    function redirectService($q, $location){ 
     ... 
     var redirect = function() { 
      ... 
     }; 

     return {   
      doRedirect: redirect 
     }; 
    } 

in meinem anderen Controller, wo ich diese redirectService bin Injektion Ich versuche, diese doRedirect Methode aufrufen veröffentlichen

angular 
    .module("myModule") 
    .controller("MyController", 
       ["$scope", 
       "redirectService"     
        MyController]); 

    function MyController(redirectService){ 
     vm.doClick = function() { 
     redirectService.doRedirect(); 
     } 
    } 

Hier erhalte ich Fehler beim Aufruf doRedirect Methode

Error: redirectService.doRedirect is not a function

+1

Könnte es sein, dass loginRedirectService Redirect sein sollte Service beim Anruf – Thargor

+0

Sie erstellen Redirect-Funktion, aber versuchen Sie redirectPostLogin zurückzugeben. Oder Sie haben den Code einfach falsch eingefügt? – speedingdeer

Antwort

2

Sie haben ein Ungleichgewicht der Anzahl der Argumente in Abhängigkeit Array und Funktionsargumente für MyController

ändern

function MyController(redirectService){ 

Um

function MyController($scope, redirectService){ 
0

MyController Funktion hat zwei Argumente ersten $ scope, zweite redirectService

angular 
    .module("myModule") 
    .controller("MyController", 
       ["$scope", 
       "redirectService"     
        MyController]); 

    function MyController($scope, redirectService){ 
     vm.doClick = function() { 
     redirectService.doRedirect(); 
     } 
    } 
Verwandte Themen