不知道为什么,但我的 Controller 无法识别该服务。我已经检查了我在这里缺少的一切。我已将服务文件包含在 HTML 中。它识别其他服务,但不仅仅是这个服务
(function() {
'use strict';
angular
.module('pollyvotes')
.service('lineChart', lineChart);
/** @ngInject */
function lineChart($scope, $http){
var promise = null;
return function(){
if (promise){
return promise
} else {
promise = $http.jsonp('')
.success(function(data){
$scope.predictions = data;
})
.error( function(data){
$scope.data = "Request failed";
})
return promise;
}
}
Controller
(function() {
'use strict';
angular
.module('pollyvotes')
.controller('MainController', MainController);
/** @ngInject */
function MainController($scope, $timeout, lineChart) {
$scope.photos = [ {id: 'chart-1', name: 'something here',src: "assets/images/300x600.png", href: "https://www.google.de/?gws_rd=ssl", discription: "say something about the chart here"},
{id: 'chart-2', name: 'another picture', src: "assets/images/300x600.png", href: "https://www.google.de/?gws_rd=ssl", discription: "say something about the chart here"},
{id: 'chart-3', name: 'another picture', src: "assets/images/300x600.png", href: "https://www.google.de/?gws_rd=ssl", discription: "say something about the chart here"},
{id: 'chart-4', name: 'another picture', src: "assets/images/300x600.png", href: "https://www.google.de/?gws_rd=ssl", discription: "say something about the chart here"}
];
}
})();
并声明模块
(function() {
'use strict';
angular
.module('pollyvotes', ['ngAnimate', 'ngCookies', 'ngTouch', 'ngSanitize',
'ngMessages', 'ngAria', 'ngResource', 'ui.router',
'ui.bootstrap', 'akoenig.deckgrid', 'smoothScroll',
'ngToast', 'picardy.fontawesome']);
})();
最佳答案
您正在将 $scope 注入(inject)到您的服务中。
文档对此错误的描述如下: https://docs.angularjs.org/error/ $注入(inject)器/unpr
Attempting to inject a scope object into anything that's not a controller or a directive, for example a service, will also throw an Unknown provider: $scopeProvider <- $scope error.
这里有一个很好的答案如何避免这种情况: Injecting $scope into an angular service function()
关于javascript - Angular 错误: [$injector:unpr] Unknown provider: lineChartProvider <- lineChart <- MainController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34947580/