"Unknown Provider" in AngularJS - angularjs

I'm trying to load a few services and controllers into an AngularJS app but I am facing an "Unknown Provider" message that I don't understand.
Unknown provider: $scope, accountManagementServiceProvider <- $scope,
accountManagementService <- RegistrationSigninControllerS/<#https:
//ajax.googleapis.com/ajax/libs/angularjs/1.3.13/an… id
My app.js is like this
var dataApp = angular.module('niwadataApp', ['ngRoute','niwaDataControllers']);
along with some config stuff and routes etc etc. Then in a separate file I define some settings and attempt to load the services/controller
dataApp.value('isEmbedded', false); //And the other 'auth' settings
dataApp.service('authenticationService', ['$http', '$q', '$rootScope', '$location', 'authTimeout', 'authTimeoutUrl', 'authUrl',AuthenticationServiceImpl]);
dataApp.factory('authenticatingProxyService', ['authenticationService', 'isEmbedded', '$http', '$q', '$window', '$rootScope', AuthenticatingProxyServiceImpl]);
dataApp.service('accountManagementService', ['$q', 'authenticatingProxyService', AccountManagementServiceImpl]);
dataApp.controller('RegistrationSigninController', ['$scope, accountManagementService', RegistrationControllerImpl]);
The classes are all defined in their own files that are loaded before app.js, e.g.
var AccountManagementServiceImpl = function($q, auth) {
According to the error message it is looking for accountManagementServiceProvider but I've got a class called accountManagementServiceImpl and that is what I've specified in the dataApp.service( call. Have I missed something in the documentation that defines class names must end with 'Provider'? Which provider does it not know about? As far as I can see each controller, service and factory defines its requirements and should be able to be created.

You are missing some quotes.
Change this:
dataApp.controller('RegistrationSigninController', ['$scope, accountManagementService', RegistrationControllerImpl]);
to this:
dataApp.controller('RegistrationSigninController', ['$scope', 'accountManagementService', RegistrationControllerImpl]);

Related

Unknown provider error when injecting AngularJS factory into controller

I'm trying to inject my factory into my controller and I'm getting this error from AngularJS:
Error: $injector:unpr Unknown Provider
I have looked through almost all of the questions on here and still cannot find a solution to my problem. I believe my controller and factory and declared correctly and the injection is correct but it looks like this isn't the case.
My factory code is as follows:
var app = angular.module('test', []);
app.factory('processingFactory', function () {
var factory = {};
factory.newTest = function() {
console.log("TEST");
}
return factory;
});
This is then injected into the controller which looks like this:
angular.module("test", ["angularModalService", "anguFixedHeaderTable",
'angular-loading-bar', "ngResource", "agGrid",
'ui.tree']).controller("dashboardController", [
"$scope",
"$timeout",
"$http",
"$window",
"$interval",
"$resource",
"ModalService",
"$filter",
'$q',
'processingFactory',
function($scope, $timeout, $http, $window, $interval, $resource,
ModalService, $filter, $q, processingFactory) {
//other code removed
$scope.newWorkorder = processingFactory.newWorkorder;
}
]);
This function is called through a button click on the web page. All of the files needed are in script tags on this html page. I am fairly new to angular so this could be a simple error or something I am not aware of.
Calling angular.module with an array as the second argument declares a module, which can only happen for any given module name. You are declaring the module twice (once in your controller code, and again in your factory code).
Try changing the first part of your factory code to:
var app = angular.module('test');
If you are doing the same thing elsewhere in the app you will need to remove the second argument there too, so that there is only one module declaration in the whole app.
if there are any dependencies for your module "test" why do not you have them declared in the first line itself like:
var app = angular.module("test", ["angularModalService", "anguFixedHeaderTable",
'angular-loading-bar', "ngResource", "agGrid",
'ui.tree']);
Then declare your controller like::
app.controler(...)
Things should work fine.

angular multiple dependency injection

This is a general dependency injection question. Obviously I am doing it wrong.
I am trying to get angular-xeditable working in my app.
https://vitalets.github.io/angular-xeditable/
I've installed it using bower, and added the appropriate script link to my head.
Now I'm trying to inject it.
The docs say
var app = angular.module("app", ["xeditable"]);
so, in my app: I do this:
portalApp.controller('portalController',
['$scope', '$http','$filter', 'xeditable',
function($scope, $http, $filter, xeditable) {
but I get the provide error, meaning it can't find xeditable.
angular.js:13642Error: [$injector:unpr] http://errors.angularjs.org/1.5.6/$injector/unpr?p0=xeditableProvider%20%3C-%20xeditable%20%3C-%20portalController
What am I doing wrong?
OK, duh. It should be
var portalApp = angular.module("portalApp", ['xeditable']);
portalApp.controller('portalController', ['$scope', '$http','$filter', function($scope, $http, $filter) {
I'm still getting lots of errors, but at least not that one.

Using ngCookies in a ionic project

I am trying to include ngCookies in a project. The angular cookies library is included in my index.html after the ionic.bundle.
I can see on the network tab of the developer tools that it is actually loading. Angular doesn't show any error when loading the page, as it usually does when a module is missing. The problem is that, when in my code I try to access the functions of the $cookies service, the $cookies variable is actually pointing to an empty object.
Here are some relevant code snippets:
On the definition of my app.js
angular.module('myApp', [
'ionic',
'ngCookies',
'ngMessages',
'rt.eventemitter',
'myApp.views']);
On my factory:
angular.module('myApp.views')
.factory('UserStore', ['$rootScope', '$q', '$cookies', '$timeout',
function($rootScope, $q, $cookies, $timeout){
var user = {};
function setSessionId(sessionId){
console.log(">> setting sessionId to:",sessionId);
user.sessionId = sessionId;
$cookies.put('sessionId', user.sessionId);
}
return{ setSessionId:setSessionId}
}
]);
In this case, when I try to call the setSessionId method I get an error that $cookies.put is not a function since, as I mentioned above, $cookies is just an empty object.
Any Ideas?
it depends on which angular version you use!
they changed a lot in angular 1.4.. in angular 1.3 when you set a cookie you can just assign it:
$cookies.sessionId = user.sessionId;

Injecting a factory in Angular

Hello I can't call a factory function. When I use it I have the next message
Here my factory
And the controller with the call
When I try to print Account, it is undefined. Can anyone see my error? Thanks
It's because Account is not declared as a dependency of the controller. Change it to:
.controller('forgotController', ['$scope', '$location', 'Account',
function($scope, $location, Account) {
For this to work, you need to make sure Account is available to the injector for your module. So when you declare the module that your controller is in, if it's not in the same module, be sure to include AccountService as a module dependency. For example:
angular.module('ForgotModule', ['AccountService'])
.controller('forgotController', ['$scope', '$location', 'Account',
function($scope, $location, Account) {

accessing $window and $logProvider in angular

When I am trying this (CoffeeScript)
.config ['$logProvider', '$window', ($logProvider, $window) ->
//...do something with parameters...
]
I get "$window not defined", and whe I try run block
.run ['$logProvider', '$window', ($logProvider, $window) ->
//...do something with parameters...
]
I get "Unknown provider: $logProviderProvider <- $logProvider"
I there any way I can use both?
Providers are only scoped at config time, then their provided service is available:
.config ['$logProvider', ($logProvider) ->
$logProvider.debugEnabled(true);
]
.run ['$log', '$window', ($log, $window) ->
$log.debug('hello logger!');
]
Note that $window is not a provider thus can't be configured.
Your answer is: .config handles pure module configuration. Once done, factories are ready so you can use everything you need. In your case, you actually need $log and $window.

Resources