Injecting a factory in Angular - angularjs

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) {

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.

AngularJS: Error: $q is not defined

I want to make a promise in my angularjs controller. I took the example from the Angularjs Doc and pasted it in my controller. When I try to run the code, the console prints:
Error: $q is not defined
Why is this error happening and how do I solve it?
I tried to google this problem, but most questions revolve about more specific problems than mine.
A (german) guide tells me that promises are already in angular js implemented and there is no need to add anything to it.
EDIT:
this is my controller:
app.controller("ArgumentationController", [
'$scope', '$resource',
function($scope, $resource) {
EDIT2:
A commentor suggested to inject $q. I did this:
app.controller("ArgumentationController", [
'$scope', '$resource', '$q',
function($scope, $resource, $q) {
Now, the error does not occur.
From your past code, no need of $resource in your code. Instead inject $q in it.
As you are creating a dummy promise using $q, make following changes.
app.controller("ArgumentationController", [
'$scope', '$q',
function($scope, $q) {

$timeout is not a function error

Hey guys got a little problem that i cant seem to see the problem for. Im building an angular application and im getting the error stated in the question title. Ive injected the $timeout to the controller but im still getting an error with this bit of code can some one tell me where i may be going wrong?
cheers
(function() {
'use strict';
angular
.module('my.module')
.controller('NewSearchController', NewSearchController);
NewSearchController.$inject = ['$rootScope', '$scope', '$location','UserService',
'SearchService', '$window', '$controller', '$mdDialog', 'ModalService', '$routeParams', '$timeout'];
/**
* #namespace ContactController
*/
function NewSearchController($rootScope, $scope, $location, UserService, SearchService, $window, $controller, $mdDialog, $routeParams, $timeout)
Timeout code:
var timerMax = false;
$scope.$watch(NewSearchController.searchObject.maxDayRate, function(){
if(timerMax) {
$timeout.cancel(timerMax);
}
timerMax= $timeout(function() {
NewSearchController.updateSearchFilters();
}, 5000);
});
The problem is in your injection: 'ModalService' listed as injectable but not one of the parameters so angular will inject 'ModalService' and the values you get for parameters later in the list are all wrong.
If you use something like gulp to build your app then use gulp-ng-annotate to build the injection list automatically. That way it won't go wrong and you never have to worry about it.
'$mdDialog', 'ModalService', '$routeParams', '$timeout']
$mdDialog, $routeParams, $timeout
these two dnt match Change it to
$mdDialog,ModalService, $routeParams, $timeout
you have injected $timeout at position 11th position in controller but in function it is at 10th position as u have missed ModalService in function.So the error is just because of this . You need to inject and add the dependcies at same position otherwise it wont work.

"Unknown Provider" in 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]);

angular-ui-router: what is the dependency for using $state in a controller?

This may be a really simple question but I can't find anything in the ui-router docs. I want to call the $state.go() method to change a state in a controller, but I get a "$state not defined" error.
What is the dependency I need to put on my controller in order to be able to use $state and its methods?
It is the same as with any other service - include it's name in annotated dependency list or function arguments:
//without annotation (inferred, not safe when minifying code)
function Controller($scope, $state) {...}
//inline annotation
module.controller('Controller', ['$scope','$state', function($scope, $state) {...}]);
//$inject property annotation
function Controller($scope, $state) {...}
Controller.$inject = ['$scope', '$state'];

Resources