user2783193 user2783193 - 5 months ago 28
AngularJS Question

calling public method from another js function inside service

I created custom 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
};
}


inside my other controller where I'm injecting this redirectService I'm trying to call this publish doRedirect method

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

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


Here I'm getting error on calling doRedirect method


Error: redirectService.doRedirect is not a function

Answer

You have an imbalance of number of arguments in dependency array and function arguments for MyController

Change

function MyController(redirectService){

To

function MyController($scope, redirectService){
Comments