2016-03-26 15 views
0

最初に角度のあるアプリケーションを実行し、異なるチュートリアルを組み合わせますが、これは初めてサービスをインジェクトしようとしています。私は開発者ツールでコンソールを見たときサービスインジェクションエラーを追加する角度モジュール

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


    } 

しかし、エラーを見ています:

angular.js:12793 Error: [$injector:unpr] Unknown provider: authServiceProvider <- authService <- signupController http://errors.angularjs.org/1.5.0-beta.2/ $injector/unpr?p0=authServiceProvider%20%3C-%20authService%20%3C-ignupController

マイプロジェクト構造は次のとおりです。

は、私のような私のビューのコントローラの1持っています

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

私の追加したindex.htmlスクリプト:

ログイン/登録取り扱う0
 <!-- 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

+0

使用することができますか? – Avi

+0

authserviceコードを共有してください – mindparse

+0

authServiveとmyAppPagesとmyAppCoreの両方に1行追加されました – Pipeline

答えて

1

あなたはVAR appを定義し、その定義するangular.module("myApp")を使用していない、あなたのfactoryまた

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

あなたがvar app = angular.module("myApp")を宣言し、あなたがmyApp.Pagesモジュールを定義しなかったapp

1

で参照エラー "アプリが定義されていない" 私は単に宣言していない見て:

VARアプリ= angular.module(...)

をと私のサービスは、それが存在しないときにアプリケーションを参照していました。

関連する問題