我创建了自定义服务
(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
};
}
在我注入(inject)此 redirectService 的其他 Controller 中,我正在尝试调用此 publish doRedirect 方法
angular
.module("myModule")
.controller("MyController",
["$scope",
"redirectService"
MyController]);
function MyController(redirectService){
vm.doClick = function() {
redirectService.doRedirect();
}
}
这里我在调用 doRedirect 方法时遇到错误
Error: redirectService.doRedirect is not a function
最佳答案
MyController
的依赖数组和函数参数中的参数数量不平衡
改变
function MyController(redirectService){
到
function MyController($scope, redirectService){
关于javascript - 从服务内的另一个 js 函数调用公共(public)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37746842/