gpt4 book ai didi

javascript - 从服务内的另一个 js 函数调用公共(public)方法

转载 作者:行者123 更新时间:2023-11-30 16:01:02 25 4
gpt4 key购买 nike

我创建了自定义服务

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com