簡體   English   中英

Angular模塊添加服務注入錯誤

[英]Angular module adding a Service injection error

第一次進行有角度的應用程序,結合不同的教程,但這是我第一次嘗試注入服務。

我有以下View控制器之一:

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


  }

但是當我在開發人員工具中查看控制台時看到錯誤:

angular.js:12793錯誤:[$ injector:unpr]未知提供程序:authServiceProvider <-authService <-signupController 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中的“未定義的應用程序”引用錯誤

我只是沒有聲明:

var app = angular.module(...)

我的服務是在不存在的情況下引用應用程序。

您沒有定義var app ,所以請使用angular.module("myApp")定義factory

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM