0

i am new to angular js i am trying to inject factory in a config ,but it shows module not found

factory code

var app=angular.module('app',['ui.router','angular-jwt','ngResource','angularValidator','satellizer']);

app.factory('AUTH_SERVICE', function ($auth,$state,$q) {
    var auth={};

 auth.isloggedin=function() {
     var defer = $q.defer();
    if($auth.isAuthenticated()){
   defer.resolve();
    }
    else
    {      
  $state.go('login');  
   defer.reject();     
    }

    return defer.promise;
}

 return auth;
});

config code is

app.config(['AUTH_SERVICE','$authProvider','$stateProvider', '$urlRouterProvider',function(AUTH_SERVICE,$stateProvider, $urlRouterProvider,$authProvider) {
$urlRouterProvider.otherwise('/');
    $stateProvider
    .state('home',{
        url:'/',
        templateUrl:'pages/home.html'

})
.state('login',{
    url:'/login',
    templateUrl:'pages/login.html',
    controller:'loginCtrl'
})
.state('register',{
    url:'/register',
    templateUrl:'pages/register.html',
    controller:'registerCtrl'

})

.state('dashboard',{
    url:'/Dashboard',
    templateUrl:'pages/dashboard.html',
    controller:'dashctrl',
    resolve:{
        skiplogin: _skiplogin
    }
})

function _skiplogin(AUTH_SERVICE){
return AUTH_SERVICEAUTH_SERVICE.isloggedin();
}



 }]);

but it shows error

angular.js:38Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.5.8/$injector/modulerr?p0=app&p1=Error%3A%20%…ocalhost%2Faccoex%2Fbower_components%2Fangular%2Fangular.min.js%3A39%3A319)

2 Answers 2

3

You can't inject factory object in the app.config because it can't be sure they have been loaded correctly..

In the app.config you can only inject Consts and Providers.

From the documentation

A module is a collection of configuration and run blocks which get applied to the application during the bootstrap process. In its simplest form the module consist of collection of two kinds of blocks:

Configuration blocks - get executed during the provider registrations and configuration phase. Only providers and constants can be injected into configuration blocks. This is to prevent accidental instantiation of services before they have been fully configured.

Run blocks - get executed after the injector is created and are used to kickstart the application. Only instances and constants can be injected into run blocks. This is to prevent further system configuration during application run time.

Take a look to provider here where you can configure a service in your app.config.

Sign up to request clarification or add additional context in comments.

2 Comments

i cannot understand ialso tried injecting the service @suren-srapyan
@ArunkarthiMani no factories, no services, no values can be injected into the .config, only providers and constants
1

you can't inject factory in config as it loads after config

angularjs application loads as follows:

Run

Config

Instantiate factory or service

Controller

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.