Unknown provider in .config() with custom Provider - angularjs

I've got this in app.js:
angular.module('App',[]).config(['TranslationProvider', function (TranslationProvider) {
//codes...
}]);
And this service in another file:
angular.module('App')
.provider('Translation', function() {
var translations = {foo:"bar"}
this.$get = function(){
return translations;
};
});
No 404 error with the service js file, but when angular injector try to instantiate it gives me this error:
Uncaught Error: [$injector:modulerr] Failed to instantiate module App due to:
Error: [$injector:unpr] Unknown provider: TranslationProvider
I've followed the angjs documentation https://docs.angularjs.org/guide/providers

It seems to be a bug in Angular 1.2.x. The order of the calls matter. When you call provide before config it works. It also works with Angular 1.3.x regardless of the order.

You are almost there.
First the config:
angular.module('App')
.config(["TranslationProvider", function(theProvider) {
console.log("in config" + theProvider)
theProvider.setName("some name");
}]);
The error you were having was due to you using the constructor function as an argument.
Now your provider:
angular.module('App')
.provider('Translation', function TranslationProvider() {
var translations = {foo:"bar", name:""};
var dynamicName;
this.setName = function(configName) {
dynamicName = configName;
};
this.$get = function(){
translations.name = dynamicName;
return translations;
};
});
Also note that an anonymous function (i.e. .provider('Translation', function () {) could also have been used and the code would have worked just as well.
Using it all in a controller:
angular.module('App')
.controller('MyController', [
'$scope',
'Translation',
function ($scope,
translationProvider) {
$scope.modal = {};
$scope.projects = [];
console.log(translationProvider)
$scope.foo = translationProvider;
}]);

Related

how to get variable from different AngularJS file?

How is it possible to get $scope variable from different file (with different module)? For example, I have two files - index.js and login.js, I want to get username from login.js in index.js. I tried to use services but couldn't achieve that goal. The controller doesn't see service in another angular file.
Codes partially are given below:
bookApp.controller('bookListCtrl', ['sharedProperties', function($scope, $http, sharedProperties) {
'use strict';
$scope.name = "Alice";
console.log("in book controller");
console.log("getting login name: "+sharedProperties.getProperty());
and
var authentication = angular.module('authentication', []);
authentication.service('sharedProperties', function () {
var property = 'First';
return {
getProperty: function () {
return property;
},
setProperty: function(value) {
property = value;
}
};
});
I got this exception -
angular.min.js:63 Error: Unknown provider: authentication.sharedPropertiesProvider <- authentication.sharedProperties
at Error (native)
at
There are 2 problems in the given implementation. The first problem is that the module 'authentication' needs to be a dependency for the consuming modules. The second problem is in the declaration of bookListCtrl. It needs to be defined as follows.
bookApp.controller('bookListCtrl', ['$scope','$http','sharedProperties', function($scope, $http, sharedProperties){
}]);
Can you give an example how you've used services?
Normally if you define controllers like:
app.controller('LoginController', ['UserService', function($scope) {
$scope.someMethod = function(){
// push information to service
UserService.username = $scope.username;
}
}]);
app.controller('IndexController', ['UserService', function($scope) {
// pull information from service
$scope.username = UserService.username;
}]);
It should work. I must suggest you thou to use Controller as instead of $scope. More info here: https://docs.angularjs.org/api/ng/directive/ngController

'Unknown provider' error while retrieving lazy-loaded controller

I want 'MyController2' to inherit 'MyController1', however, both controllers are lazyloaded using ocLazyLoad. According to Jussi Kosunen's answer to this question (https://stackoverflow.com/a/15292441/2197555), I have made a function registerDynamic() to register the controllers, but it still reports the following error at the line with '$controller' in controller1.js:
Error: [$injector:unpr] Unknown provider: $elementProvider <- $element <- DataTableController
My codes are like this.
First file controller1.js:
angular.module( 'myApp',[])
.controller( 'MyController1', [ function($scope){
// ...
}]);
Second file controller2.js:
angular.module( 'myApp')
.controller( 'MyController2', [ '$controller', '$scope',function($controller, $scope){
$controller('MyController1', {$scope: $scope }); // here triggers error '[$injector:unpr] Unknown provider'
// ...
}]);
In the third File lazy-load.js, I lazyload the above two .js files:
var app = angular.module('myApp'),
queueLen = app._invokeQueue.length;
app.directive( 'LazyLoad', [ function( ){
return {
restrict: 'EA',
scope: {
src: '#',
},
link: function( scope, element, attr ){
var registerDynamic = function() {
// Register the controls/directives/services we just loaded
var queue = syncreonApp._invokeQueue;
for(var i=queueLen;i<queue.length;i++) {
var call = queue[i];
// call is in the form [providerName, providerFunc, providerArguments]
var provider = syncreonApp.providers[call[0]];
if(provider) {
// e.g. $controllerProvider.register("Ctrl", function() { ... })
$log.debug("Registering " + call[1] + " " + call[2][0] + " ...");
provider[call[1]].apply(provider, call[2]);
}
}
queueLen = i;
},
loadMultipleJs = function ( js_files ){
var deferred = $q.defer();
var js_file1 = js_files.shift(),
js_file2 = js_files.shift();
$ocLazyLoad.load( js_file1 )
.then ( function(){
registerDynamic();
$ocLazyLoad.load( js_file2 )
.then ( function(){
registerDynamic();
deferred.resolve();
}, function(){
deferred.reject();
});
}, function(){
deferred.reject();
});
};
jsonOfJsFilesToLoad = JSON.parse(scope.src);
loadMultipleJs(jsonOfJsFilesToLoad );
}
};
}]);
UPDATE
The official Angular documentation for 'Unknown Provider' error says:
Attempting to inject one controller into another will also throw an Unknown provider error:
Maybe we just cannot injector controller into anther even using $controller service?
You are taking the error message out of context.
Attempting to inject one controller into another will also throw an Unknown provider error:
angular.module('myModule', [])
.controller('MyFirstController', function() { /* ... */ })
.controller('MySecondController', ['MyFirstController', function(MyFirstController) {
// This controller throws an unknown provider error because
// MyFirstController cannot be injected.
}]);
That is not the way you are instantiating controllers.
It specifically says:
Use the $controller service if you want to instantiate controllers yourself.
Which is the way you are instantiating your controllers.
Look for your problem elsewhere.
Your error message:
Unknown provider: $elementProvider <- $element <- DataTableController
The way I read this is that in your DataTableController, you are trying to inject $element. $element is not a service is a local. To inject $element as a local with the $controller service:
$controller('DataTableController', {$scope: $scope, $element: value });

Angular Service injected not working

I have created and injected the service(myService) into my app (app) , but it is not working. The error implies that I have not defined the service anywhere:
Error: [$injector:unpr] Unknown provider: myServiceProvider <- myService <- myController
myService calls another service - ajaxService to do the actual http call.
The only reason I would think that myService throws the above error when trying to call it in myController is because I have another module defined in the app definition (common.components). This module has its own separate services which I am using elsewhere in my app. I am wondering if the app is searching for a definition of myService within that the common.components module instead of inside itself.
Here is my code:
- app.js
var app = angular.module('app ', ['ngRoute','common.components']);
- myService.js
var serviceId = 'myService';
angular.module('app').service(serviceId,['$q','ajaxService','$log',myService]);
function myService($q, ajaxService, $log){
var states = [];
this.getStates = function() {
var defered = $q.defer();
ajaxService.getStates().then(function(result){
states = result.data;
defered.resolve(states);
},
function(error){
deferred.reject();
});
return defered.promise;
};
}
- ajaxService.js
var serviceId = 'ajaxService';
angular.module('app',[]).service(serviceId,['$http','$log',ajaxService]);
function ajaxService($http,$log){
this.getStates = function() {
return $http.get('./json/DATA.json');
};
}
myController.js
(function(){
'use strict';
angular.module('app').controller('myController',['$scope','$log','myService',myController]);
function myController($scope,$log,myService){
$scope.states = [];
myService.getStates().then(function(states){
$scope.states = states;
});
}
})();
I have been trying to find out what is wrong for hours, but I am lost. Can someone help me with this?
I have updated my answer as you have now provided more info.
Your issue is in your ajaxService.js
Change this line
angular.module('app',[]).service(serviceId,['$http','$log',ajaxService]);
to this
angular.module('app').service(serviceId,['$http','$log',ajaxService]);
Your are recreating the app module by adding the [].

Error: [$injector:unpr] Unknown provider: in AngularJS Service Test

I am having a lot of trouble getting dependencies provided properly for an AngularJS service.
I see a number of other posts with similar errors here on StackOverflow but none of them seem to resolve the issue.
Here is the app code:
cm.modules.app = angular.module('myApp', ['ngRoute', 'ngAnimate']);
myServiceName = function($http) {
// do stuff
};
myServiceName.prototype.value = 1;
cm.modules.app.service('defaultAlertFactoryA', myServiceName);
Here is the test code:
describe('test alertFactoryA', function() {
var $provide;
var mAlertFactoryA;
beforeEach(module(cm.modules.app));
beforeEach(angular.mock.module(function(_$provide_) {
$provide = _$provide_;
}));
beforeEach(function() {
inject(function($injector) {
mAlertFactoryA = $injector.get('defaultAlertFactoryA');
});
});
it('should work', function() {
expect(true).toBe(true);
});
});
Here is the error:
Error: [$injector:unpr] Unknown provider: defaultAlertFactoryAProvider
<- defaultAlertFactoryA
http://errors.angularjs.org/1.2.0-rc.2/$injector/unpr?p0=defaultAlertFactoryAProvider%20%3C-%20defaultAlertFactoryA
Question: How do I fix this so the test passes?
In order to bootstrap your module you need to provide its name
beforeEach(module('myApp'));
Demo
The following is what I used to get it working (finally)
beforeEach(function() {
module(cm.modules.app.name);
module(function($provide) {
$provide.service('defaultAlertFactoryA', myServiceName);
});
inject(function($injector) {
defaultAlertFactory = $injector.get('defaultAlertFactoryA');
});
});
Sounds like you need to include the service files in your karma.conf.js file
files: [
'app/bower_components/angular/angular.js',
'app/bower_components/angular-mocks/angular-mocks.js',
'app/bower_components/angular-ui-router/release/angular-ui-router.js',
'app/app.js',
'app/controllers/*.js',
'app/services/*.js',
'tests/**/*.js'
],
If the are not included here they can't be accessed in the unit tests

AngularJS use custom services in custom providers

I have a simple question about the dependency injection in Angular. I create custom services in order to use them within each other. Unfortunately I receive errors the way I was trying it. This is my Code:
var myApp = angular.module('app', []);
myApp.service('$service1', ['$rootScope', function($rootScope) {
this.test = function() {
console.log('service1');
};
}]);
myApp.provider('$service2', ['$service1', function($service1) {
var service = 'service2';
this.registerService = function(mytext) {
service = mytext;
};
this.$get = function() {
var that = {};
that.test = function() {
console.log(service);
};
return that;
};
}]);
myApp.config(['$service2Provider', function($service2Provider) {
$service2Provider.registerService('changed service2');
}]);
myApp.controller('AppCtrl', ['$rootScope', '$service1', '$service2',
function($rootScope, $service1, $service2) {
$service1.test();
$service2.test();
}]);
Error: [$injector:modulerr] Failed to instantiate module app due to:
[$injector:unpr] Unknown provider: $service1
http://errors.angularjs.org/1.2.0-rc.2/$injector/unpr?p0=%24service1
If you remove the dependency of $servic1 in $service2 it will work, but why?
The code is mostly right, except you have to inject service dependencies in $get, not in the provider constructor function, like this:
myApp.provider('$service2', function() {
var service = 'service2';
this.registerService = function(mytext) {
service = mytext;
};
this.$get = ['$service1', function($service1) {
var that = {};
that.test = function() {
console.log(service);
};
return that;
}];
});
It appears that provider can not inject such a dependency. If you rewrite $service2 using a factory, it works:
myApp.factory('$service2', ['$service1', function($service1) {
var that = {};
that.test = function() {
$service1.test();
console.log('service2');
};
return that;
}]);
See this plunker: http://plnkr.co/edit/JXViJq?p=preview
Also I believe that service names starting with a $ a reserved for AngularJS and its extensions. Use names without the $ at the beginning for services defined by your application.

Resources