我试图通过遵循工厂方法文档中的示例来构建自己的服务。我认为我做错了,因为我继续遇到未知的提供程序错误。这是我的应用程序代码,包括声明,配置和工厂定义。
编辑我现在添加了所有文件以帮助解决问题
编辑该错误的完整详细信息在下面,问题似乎与getSettings有关,因为它正在寻找getSettingsProvider,但找不到它
Error: [$injector:unpr] http://errors.angularjs.org/1.2.16/$injector/unpr? p0=getSettingsProvider%20%3C-%20getSettings at Error (native) at http://localhost/sw/selfservice/bower_components/angular/angular.min.js:6:450 at http://localhost/sw/selfservice/bower_components/angular/angular.min.js:35:431 at Object.c [as get] (http://localhost/sw/selfservice/bower_components/angular/angular.min.js:34:13) at http://localhost/sw/selfservice/bower_components/angular/angular.min.js:35:499 at c (http://localhost/sw/selfservice/bower_components/angular/angular.min.js:34:13) at d (http://localhost/sw/selfservice/bower_components/angular/angular.min.js:34:230) at Object.instantiate (http://localhost/sw/selfservice/bower_components/angular/angular.min.js:34:394) at http://localhost/sw/selfservice/bower_components/angular/angular.min.js:66:112 at http://localhost/sw/selfservice/bower_components/angular/angular.min.js:53:14 angular.js:9778 (anonymous function) angular.js:9778 (anonymous function) angular.js:7216 h.$apply angular.js:12512 (anonymous function) angular.js:1382 d angular.js:3869 $b.c angular.js:1380 $b angular.js:1394 Wc angular.js:1307 (anonymous function) angular.js:21459 a angular.js:2509 (anonymous function) angular.js:2780 q angular.js:330 c
这些是我目前在我的应用程序中拥有的所有文件
app.js
//Initialize angular module include route dependencies var app = angular.module("selfservice", ['ngRoute']); app.config(function ($routeProvider) { $routeProvider .when('/', { templateUrl:"partials/login.html", controller:"login" }); }); app.factory('getSettings', ['$http', '$q', function($http, $q) { return function (type) { var q = $q.defer(); $http.get('models/settings.json').success(function (data) { q.resolve(function() { var settings = jQuery.parseJSON(data); return settings[type]; }); }); return q.promise; }; }]);
这就是我在控制器中使用此服务的方式
控制器
app.controller("globalControl", ['$scope','getSettings', function ($scope,getSettings) { var loadSettings = getSettings('global'); loadSettings.then(function(val) { $scope.settings = val; }); }]); app.controller("login", ['$scope', function ($scope) { return "" }]);
指令.js
app.directive('watchResize', function(){ return { restrict: 'M', link: function(scope, elem, attr) { scope.spacer = (window.innerWidth < 1025) ? '' : 'large-3'; scope.button = (window.innerWidth < 1025) ? '' : 'large-6'; angular.element(window).on('resize', function(){ scope.$apply(function(){ scope.spacer = (window.innerWidth < 1025) ? '' : 'large-3'; scope.button = (window.innerWidth < 1025) ? '' : 'large-6'; }); }); } }; });
如果相关,这里是HTML
<html class="no-js" lang="en" ng-app="selfservice" ng-controller="globalControl"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>{{settings.title}}</title> <link rel="stylesheet" href="css/app.css" /> <script src="bower_components/modernizr/modernizr.js"></script> <script src="bower_components/angular/angular.min.js"></script> <script src="bower_components/angular-route/angular-route.min.js"></script> <script src="bower_components/jquery/dist/jquery.min.js"></script> <script src="js/app.js"></script> <script src="js/controllers.js"></script> <script src="js/directives.js"></script> </head> <body> <div id="template"> <header id="header"> <img src="{{settings.logo}}" alt="{{settings.logoDescription}}"/> </header> <div id="view"> <ng-view></ng-view> </div> </div> <script src="bower_components/foundation/js/foundation.min.js"></script> <script> //initialize foundation $(document).foundation(); </script> </body> </html>
有人可以指出我正确的方向吗?我已竭尽全力按照文档进行操作,因此对大多数相关问题进行了更深入的了解,而且对我而言更难理解。这是我第一次创建服务。
您的角度模块需要正确初始化。app需要正确定义和初始化全局对象以注入服务。
app
请参阅以下示例代码以供参考:
var app = angular.module('SampleApp',['ngRoute']); //You can inject the dependencies within the square bracket app.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) { $routeProvider .when('/', { templateUrl:"partials/login.html", controller:"login" }); $locationProvider .html5Mode(true); }]); app.factory('getSettings', ['$http', '$q', function($http, $q) { return { //Code edited to create a function as when you require service it returns object by default so you can't return function directly. That's what understand... getSetting: function (type) { var q = $q.defer(); $http.get('models/settings.json').success(function (data) { q.resolve(function() { var settings = jQuery.parseJSON(data); return settings[type]; }); }); return q.promise; } } }]); app.controller("globalControl", ['$scope','getSettings', function ($scope,getSettings) { //Modified the function call for updated service var loadSettings = getSettings.getSetting('global'); loadSettings.then(function(val) { $scope.settings = val; }); }]);
示例HTML代码应如下所示:
<!DOCTYPE html> <html> <head lang="en"> <title>Sample Application</title> </head> <body ng-app="SampleApp" ng-controller="globalControl"> <div> Your UI elements go here </div> <script src="app.js"></script> </body> </html>
请注意,控制器不是绑定到HTML标签,而是绑定到body标签。另外,请尝试在HTML页面末尾包含您的自定义脚本,因为出于性能原因,这是要遵循的标准做法。
我希望这可以解决您的基本注射问题。