gpt4 book ai didi

angularjs - "Error: Unknown provider"试图在 Angular.JS 中实现依赖注入(inject)

转载 作者:行者123 更新时间:2023-12-01 01:05:52 25 4
gpt4 key购买 nike

我是 Angular.JS 的新手,我正在尝试进行依赖注入(inject),但我得到了这个:

// Definition
app.factory('Reload', function (load, $timeout, timeout) {
if (!timeout) {
timeout = 15 * 1000; // reload page every quater minute by default
}
return $timeout(load, timeout);
});


// Controller
app.controller('SomeController', function ($scope, $routeParams, $location, $timeout, Installer, Reload) {
Reload(load, $timeout, 1000);
});

Error: Unknown provider: loadProvider <- load <- Reload
at Error (<anonymous>)
at http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js:2734:15
at Object.getService [as get] (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js:2862:39)
at http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js:2739:45
at getService (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js:2862:39)
at Object.invoke (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js:2880:13)
at http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js:2740:37
at getService (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js:2862:39)
at invoke (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js:2880:13)
at Object.instantiate (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js:2914:23)

我错过了什么?谢谢

最佳答案

Reload 的定义factory 在工厂定义和它返回的函数之间混淆了。如下更新您的代码。

// Definition
app.factory('Reload', function ($timeout) {
return function(load, timeout) {
if (!timeout) {
timeout = 15 * 1000; // reload page every quater minute by default
}
return $timeout(load, timeout);
}
});


// Controller
app.controller('SomeController', function ($scope, $routeParams, $location, $timeout, Installer, Reload) {
Reload(load, 1000);
});

关于angularjs - "Error: Unknown provider"试图在 Angular.JS 中实现依赖注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18727130/

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