【问题标题】:Angular module adding a Service injection errorAngular 模块添加服务注入错误
【发布时间】:2016-07-14 08:55:00
【问题描述】:

第一次做一个角度应用程序,结合不同的教程,但这是我第一次尝试注入服务。

我有一个 View 的控制器,例如:

  angular.module("myApp.Pages").controller('signupController', ['$scope', '$location', '$timeout', 'authService', function ($scope, $location, $timeout, authService) {


  }

但是,当我在开发者工具中查看控制台时,我看到了一个错误:

angular.js:12793 错误:[$injector:unpr] 未知提供者: authServiceProvider http://errors.angularjs.org/1.5.0-beta.2/$injector/unpr?p0=authServiceProvider%20%3C-%20authService%20%3C-ignupController

我的项目结构是:

 -Client
   -App
     -Components
       -Services 
         -authService.js
       -myAppCore.js
     -Views
     -app.js
     -appRouting.js
   -Scripts (References)
   -Theme (Css)
   -Index.html

我添加的 index.html 脚本:

     <!-- Angular References-->
<script src="References/Angular/angular.js"></script>
<script src="References/Angular/angular-route.js"></script>
<script src="References/Angular/angular-ui-router.min.js"></script>
<!-- End Angular References-->

<!-- my app and dependent modules -->
<script src="App/app.js"></script>
<script src="App/appRouting.js"></script>

<!-- Services -->
<script src="App/Components/Services/authService.js"></script>
<!-- END services-->

<!-- Controllers for your pages-->
<script src="App/Pages/Home/homeController.js"></script>
<script src="App/Pages/ContactUs/contactusController.js"></script>
<script src="App/Pages/Entry/entryController.js"></script>
<script src="App/Pages/Signup/signupController.js"></script>
<!-- End Controllers for the page-->

我的 app.js

 angular.module("myApp", [
   // User defined modules
   'myApp.Templates', // templates
   'myApp.Pages', // Pages
   'myApp.Core', // Core

   // Angular modules
   'ui.router', // state routing
   'ngRoute', // angular routing
   'angular-loading-bar', //loading bar
   'LocalStorageModule', //local browser storage
])

和 appRouting.js

 angular.module("myApp")
   .config(["$stateProvider", function ($stateProvider) {
    $stateProvider.state('Home', {
        url: '/Home',
        templateUrl: 'App/Pages/Home/home.html',
        controller: 'homeController'
    })
    .state('Entry', {
        url: '/Entry',
        templateUrl: 'App/Pages/Entry/entry.html',
        controller: 'entryController'
    })
    .state('Signup', {
        url: '/Signup',
        templateUrl: 'App/Pages/Signup/signup.html',
        controller: 'signupController'
    })
    .state('Contactus', {
        url: '/Contactus',
        templateUrl: 'App/Pages/ContactUs/contactus.html',
        controller: 'contactusController'
    })
    .state("otherwise", {
        url: "*path",
        templateUrl: "App/Pages/NotFound/notFound.html"
    });
}])

.run(["$location", function ($location) {
    // Go to state dashboard
    $location.url('/Home');

}]);

authService 处理登录/注册:

 app.factory('authService', ['$http', '$q', 'localStorageService', function ($http, $q, localStorageService) {

var serviceBase = '<location>';
var authServiceFactory = {};

var _authentication = {
    isAuth: false,
    userName: ""
};

var _saveRegistration = function (registration) {

    _logOut();

    return $http.post(serviceBase + 'api/account/register', registration).then(function (response) {
        return response;
    });

};

var _login = function (loginData) {

    var data = "grant_type=password&username=" + loginData.userName + "&password=" + loginData.password;

    var deferred = $q.defer();

    $http.post(serviceBase + 'token', data, { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }).success(function (response) {

        localStorageService.set('authorizationData', { token: response.access_token, userName: loginData.userName });

        _authentication.isAuth = true;
        _authentication.userName = loginData.userName;

        deferred.resolve(response);

    }).error(function (err, status) {
        _logOut();
        deferred.reject(err);
    });

    return deferred.promise;

};

var _logOut = function () {

    localStorageService.remove('authorizationData');

    _authentication.isAuth = false;
    _authentication.userName = "";

};

var _fillAuthData = function () {

    var authData = localStorageService.get('authorizationData');
    if (authData) {
        _authentication.isAuth = true;
        _authentication.userName = authData.userName;
    }

}

authServiceFactory.saveRegistration = _saveRegistration;
authServiceFactory.login = _login;
authServiceFactory.logOut = _logOut;
authServiceFactory.fillAuthData = _fillAuthData;
authServiceFactory.authentication = _authentication;

return authServiceFactory;
}]);

myAppPages.js 和 myAppCore.js 只是它们各自的名称相同:

 angular.module("myApp.Pages", []);

编辑:在 authService 中看到“应用未定义”引用错误

【问题讨论】:

  • 你定义了 myApp.Pages 模块吗?
  • 请分享您的授权服务代码
  • 在 myAppPages 和 myAppCore 中添加了 authServive 和一行
  • 你在哪里定义了 var app
  • 我认为这可能是问题所在,我认为我没有在任何地方声明它。我只是目前正在做:angular.module

标签: javascript angularjs


【解决方案1】:

我只是没有声明:

var app = angular.module(...)

当应用不存在时,我的服务正在引用该应用。

【讨论】:

    【解决方案2】:

    你没有定义var app,所以用angular.module("myApp")来定义你的factory

    angular.module("myApp").factory('authService', ['$http', '$q', 'localStorageService', function ($http, $q, localStorageService)
    

    您也可以声明var app = angular.module("myApp") 并使用app

    【讨论】:

      猜你喜欢
      • 2015-09-11
      • 2015-12-02
      • 2016-05-30
      • 1970-01-01
      • 2020-06-13
      • 2016-08-30
      • 2019-01-22
      • 1970-01-01
      • 2017-08-12
      相关资源
      最近更新 更多