【发布时间】:2016-11-20 13:45:13
【问题描述】:
我正在尝试使用 cookie,但出现以下错误。
ionic.bundle.js:25642 错误:[$injector:unpr] 未知提供者: UserServiceProvider
我做了以下事情:
1) 在我的 index.html 中提到了这一点
<script src="//code.angularjs.org/1.4.3/angular-cookies.js"></script>
2) 在我的 app.js 中我添加了 ngCookies
angular.module('app', ['ionic', 'app.controllers', 'app.routes', 'app.services', 'app.directives','app.filters','ngSanitize','ngCookies','base64'])
3) 然后我创建了一个新服务,我在我的索引中引用了它。
<script src="js/authenticationService.js"></script>
4) 我在 authenticationService.js 中包含了 $cookies
(function () {
'use strict';
angular
.module('app')
.factory('AuthenticationService', AuthenticationService);
AuthenticationService.$inject = ['$http', '$cookieStore', '$rootScope', '$timeout', 'UserService','$cookies'];
function AuthenticationService($http, $cookieStore, $rootScope, $timeout, UserService,$cookies) {...
有人可以帮我解决这个错误吗?
当我将 Angular cookie 版本更改为 1.2.20 时,我得到了这个错误
ionic.bundle.js:25642 TypeError: $browser.addPollFn is not a function 在对象。 (angular-cookies.js:60)
【问题讨论】:
-
问题似乎出在 UserService 上。您可以发布代码吗?
-
@m22an 这是我第一次构建应用程序,我正在“边学习边学习”。请原谅我的愚蠢问题,但你能告诉我 UserService 是什么吗?你是说我创建的 authentication.js 文件吗?
-
在您的 authenticationService.js 中,您有这一行
AuthenticationService.$inject = ['$http', '$cookieStore', '$rootScope', '$timeout', 'UserService','$cookies'];。这意味着在初始化此服务时,应该有可用于 Angular 的可注入组件,例如服务/工厂/控制器。在这种情况下,除了UserService之外的所有组件都可以作为 angular.js 的一部分或作为其他包(如angular-cookies.js)使用。您能否也为 UserService.js 添加一个定义,然后您的应用程序应该可以工作。 -
@m22an 是的,这行得通!谢谢你:)
-
很高兴为您提供帮助,如果它解决了您的问题,请接受答案。我还建议您也阅读一些关于 Angular 的一般知识。
标签: javascript angularjs cookies ionic-framework