gpt4 book ai didi

javascript - AngularJS 服务的另一个 'Unknown provider'

转载 作者:行者123 更新时间:2023-11-30 09:42:52 25 4
gpt4 key购买 nike

我其实很讨厌成为那个人,但我一直在忍受这个问题几天了。我将这三个文件作为更大的 angularjs 应用程序。我什至无法通过这种基本测试通过(甚至工作)。我一直在比较项目中的文件,我已经在线阅读(尝试了人们建议的所有方式)。我有甚至从头开始写了几次文件。我可能做不到看到我的错误了。我想这更容易发现(马上)对于后座司机。

如有任何帮助,我将不胜感激。

gulp/karma 的输出

PhantomJS 2.1.1 (Linux 0.0.0) SiteDescriptionService the service should be defined FAILED
Error: [$injector:unpr] Unknown provider: SiteDescriptionServiceProvider <- SiteDescriptionService
http://errors.angularjs.org/1.5.8/$injector/unpr?p0=SiteDescriptionServiceProvider%20%3C-%20SiteDescriptionService (line 4511)
bower_components/angular/angular.js:4511:86
getService@bower_components/angular/angular.js:4664:46
bower_components/angular/angular.js:4516:48
getService@bower_components/angular/angular.js:4664:46
injectionArgs@bower_components/angular/angular.js:4688:68
invoke@bower_components/angular/angular.js:4710:31
workFn@bower_components/angular-mocks/angular-mocks.js:3085:26
loaded@http://localhost:8080/context.js:151:17
inject@bower_components/angular-mocks/angular-mocks.js:3051:28
app/service/sitedescriptor-service-test.js:10:19
app/service/sitedescriptor-service-test.js:4:13
global code@app/service/sitedescriptor-service-test.js:1:9
Expected undefined to be truthy.
app/service/sitedescriptor-service-test.js:17:32
loaded@http://localhost:8080/context.js:151:17

模块声明

(function(){
'use strict';

angular.module('application.service', []);

})();

服务本身

(function () {
angular.module('application.service')
.service('SiteDescriptorService',
['$http', '$q', function ($http, $q) {
var lastRequestFailed = true,
promise,
items = [];

return {
name: 'SiteDescriptorService',

getItems: function () {
if (!promise || lastRequestFailed) {
promise = $http.get('site.json').then(
function (response) {
lastRequestFailed = false;
items = response.data;
return items;
}, function (response) { // error
lastRequestFailed = true;
return $q.reject(response);
});
}
return promise;
}
};
}]
);

})();

和测试

describe('SiteDescriptionService', function() {
'use strict';

describe('the service', function() {

var service, httpBackend;

beforeEach(module('application.service'));

beforeEach(inject(function(_SiteDescriptionService_, $httpBackend) {
service = _SiteDescriptionService_;
httpBackend = $httpBackend;
console.log(service);
}));

it('should be defined', function() {
expect(service).toBeTruthy();
});

});
});

干杯垫子

最佳答案

看起来你只是在注入(inject)依赖项时使用了错误的名称,应该是“SiteDescriptorService”而不是“SiteDescriptionService”

关于javascript - AngularJS 服务的另一个 'Unknown provider',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40321320/

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