Provider dependancy not working in Config AngularJS -


my code given below. below code shows dependancy error when executes following code. great. cookies dependancies required...

error

uncaught error: [$injector:modulerr] http://errors.angularjs.org/1.3.15/$injector/modulerr?p0=achieverpayroll&p1…a%2f%2flocalhost%3a8080%2fachieverpayroll%2fjs%2fangular.min.js%3a17%3a381) 

code app.js

(function(){     var app=angular.module('achieverpayroll',['ngroute']);       app.provider('loginchek',function(){         this.logfn=function(){             console.log('test');         };     });      app.config(['$routeprovider', '$httpprovider','loginchekprovider', function($routeprovider, $httpprovider,loginchekprovider){          loginchekprovider.logfn();          $routeprovider.when('/home',{             templateurl: 'templates/home.html',             controller: 'categorycontroller'         }).         otherwise({             redirectto: '/home'         });     }]);     app.controller('categorycontroller', function($scope, $http) {      });  })(); 

html:

<!doctype html> <html ng-app="achieverpayroll"> <head>     <meta charset="iso-8859-1">     <meta http-equiv="x-ua-compatible" content="ie=10">     <link href="css/style.css" rel="stylesheet" type="text/css"/>     <script src="js/angular.min.js" type="text/javascript"></script>     <script type="text/javascript" src="js/angular-route.min.js"></script>     <script type="text/javascript" src="js/angular-cookies.js"></script>     <script src="js/app.js" type="text/javascript"></script> 

....

whenever angular error , not decode error message means. try load non min version of angular , provide more descriptive error message, example in case might see like:

uncaught error: [$injector:modulerr] failed instantiate module plunker due to: error: [$injector:pget] provider 'loginchek' must define $get factory method.

which evident provider not have service constructor associated it. defines provider function that can accessed during config phase not enough. example:

app.provider('loginchek',function(){       var loggedin = false;         //this accessible during config phase before         //service logincheck instantiated         this.logfn=function(){             loggedin = true;             console.log('test');         };         //when inject logincheck anywhere else service instance         //with 1 method hasloggedin         this.$get = function(){           return {             //you can inject services stuffs here             //hasloggedin:function($http, $q...)             hasloggedin:function(){               return loggedin;             }           }         }     }); 

plnkr

doc says:

you should use provider recipe when want expose api application-wide configuration must made before application starts. interesting reusable services behavior might need vary between applications.

provider method logfn cannot make use of services because services not instantiated yet (for example cannot inject $http service directly in provider function, i.e .provider('loginchek',function($http){), can inject other providers need. used simple configuration service.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -