AngularJs service not found - angularjs

I'm trying to inject a service into a controller but the service is not found error:
angular.js:13424 Error: [$injector:unpr] http://errors.angularjs.org/1.5.3/$injector/unpr?p0=<section data-ui-view="" class="view-container {{main.pageTransition.class}} ng-scope" data-ng-animate="1">copeProvider%20%3C-%20%24scope%20%3C-%20authenticationService
at Error (native)
at http://localhost:3000/bower_components/angular/angular.min.js:6:416
at http://localhost:3000/bower_components/angular/angular.min.js:43:7
at Object.d [as get] (http://localhost:3000/bower_components/angular/angular.min.js:40:270)
at http://localhost:3000/bower_components/angular/angular.min.js:43:69
at d (http://localhost:3000/bower_components/angular/angular.min.js:40:270)
at e (http://localhost:3000/bower_components/angular/angular.min.js:41:1)
at Object.instantiate (http://localhost:3000/bower_components/angular/angular.min.js:41:364)
at Object.<anonymous> (http://localhost:3000/bower_components/angular/angular.min.js:42:184)
at Object.invoke (http://localhost:3000/bower_components/angular/angular.min.js:41:295)
My controller:
(function () {
'use strict';
angular.module('app.page')
.controller('authCtrl', ['$scope', '$window', '$location','authenticationService', authCtrl]);
function authCtrl($scope, $window, $location, authenticationService) {
$scope.login = function() {
authenticationService.test();
}
$scope.signup = function() {
$location.url('/')
}
$scope.reset = function() {
$location.url('/')
}
$scope.unlock = function() {
$location.url('/')
}
}
})();
Service:
(function () {
'use strict';
angular.module('app.page')
.service('authenticationService', ['$scope', '$window', authenticationService]);
function authenticationService() {
this.test = function()
{
alert('test');
}
}
})();
What am I doing wrong here?

The number of injects in the service call ['$scope', '$window', authenticationService] does not match the argument list of your service function ().
To avoid errors like this, I suggest you to use $inject (https://docs.angularjs.org/guide/di#-inject-property-annotation):
(function () {
'use strict';
angular.module('app.page').service('authenticationService', authenticationService);
authenticationService.$inject = [];
function authenticationService() {
this.test = function()
{
alert('test');
}
}
})();

Related

Angular-Jasmine injecting a service into the test

New to Jasmine, I am trying to instantiate my controller which has a list of dependencies (mainly the services I have written) and all the different ways I';ve tried haven't been right.
Here is my controller:
(function () {
'use strict';
angular.module('app.match')
.controller('MatchController', MatchController);
MatchController.$inject = ['APP_CONFIG', '$authUser', '$http', '$rootScope', '$state', '$stateParams', 'SearchService', 'ConfirmMatchService', 'MusicOpsService', 'ContentOpsService', 'MatchstickService', 'MatchService', 'Restangular'];
function MatchController(APP_CONFIG, $authUser, $http, $rootScope, $state, $stateParams, searchService, confirmMatchService, musicOpsService, contentOpsService, matchstickService, matchService, Restangular) {
var vm = this;
vm.greeting = '';
.
.
)();
Here is my test
(function(){
'use strict';
describe('app module', function() {
var MatchController;
//beforeEach(module('app.match'));
beforeEach(function($provide) {
module = angular.module('app.match');
$provide.service('SearchService', function(){
});
});
beforeEach(module('app.config'));
beforeEach(module('auth'));
beforeEach(inject(function($controller, APP_CONFIG, $authUser, $http, $rootScope, $state, $stateParams) {
MatchController = $controller('MatchController', {'APP_CONFIG':APP_CONFIG, '$authUser':$authUser, '$http':$http, '$rootScope':$rootScope, '$state':$state, '$stateParams':$stateParams, '$provide':$provide});
}));
describe("Match controller", function() {
it("should be created successfully", function() {
expect(MatchController).toBeDefined();
});
});
});
})();
Running test the above way gives me the following error:
TypeError: 'undefined' is not a function (evaluating '$provide.service('SearchService', function(){
})')
Try injecting the SearchService like this instead of using beforeEach.
describe('app module', function() {
var MatchController, SearchService;
beforeEach(module('app.match'));
beforeEach(module('app.config'));
beforeEach(module('auth'));
beforeEach(inject(function($controller, APP_CONFIG, $authUser, $http, $rootScope, $state, $stateParams, _SearchService_) {
SearchService = _SearchService_;
MatchController = $controller('MatchController', {
'APP_CONFIG':APP_CONFIG,
'$authUser':$authUser,
'$http':$http,
'$rootScope':$rootScope,
'$state':$state,
'$stateParams':$stateParams,
'$provide':$provide,
'SearchService': _SearchService_
});
}));
describe("Match controller", function() {
it("should be created successfully", function() {
expect(MatchController).toBeDefined();
});
});
});
})();
Similarly, you'll have to inject other services as well that your controller is relying on.

AngularJs use service from another module

I'm trying to use a service from my main module that I'm going to use everywhere.
This is my controller where I would like to use it:
(function () {
'use strict';
angular.module('app.page')
.controller('authCtrl', ['$scope', '$window', '$location','requestService', authCtrl]);
function authCtrl($scope, $window, $location, requestService) {
$scope.login = function() {
requestService.test();
}
}
})();
module:
(function () {
'use strict';
angular.module('app.page',['app']);
})();
Service
(function () {
'use strict';
angular.module('app')
.service('requestService',requestService);
function requestService() {
this.test = function()
{
alert('test');
}
}
})();
But the requestService() is not found in my controller. What am I doing wrong here?
--EDIT--
error message:
angular.js:13424 Error: [$injector:unpr] http://errors.angularjs.org/1.5.3/$injector/unpr?p0=requestServiceProvider%20%3C-%20requestService%20%3C-%20authCtrl
at Error (native)
at http://localhost:3000/bower_components/angular/angular.min.js:6:416
at http://localhost:3000/bower_components/angular/angular.min.js:43:7
at Object.d [as get] (http://localhost:3000/bower_components/angular/angular.min.js:40:270)
at http://localhost:3000/bower_components/angular/angular.min.js:43:69
at d (http://localhost:3000/bower_components/angular/angular.min.js:40:270)
at e (http://localhost:3000/bower_components/angular/angular.min.js:41:1)
at Object.invoke (http://localhost:3000/bower_components/angular/angular.min.js:41:86)
at S.instance (http://localhost:3000/bower_components/angular/angular.min.js:88:235)
at n (http://localhost:3000/bower_components/angular/angular.min.js:64:174) <section data-ui-view="" class="view-container {{main.pageTransition.class}} ng-scope" data-ng-animate="1">
When you defined your service module you didn't provide an empty dependency array to create a new module. Angular will try to find an existing app module and not find it. You need to define your service module like this:
(function () {
'use strict';
angular.module('app', [])
.service('requestService',requestService);
function requestService() {
this.test = function()
{
alert('test');
}
}
})();

angularjs : issue with injecting $uibModalInstance

I have a controller :
(function() {
'use strict';
angular
.module('myApp')
.controller('ConsultantController', ConsultantController);
ConsultantController.$inject = ['$scope', '$state', 'Consultant'];
function ConsultantController ( $scope, $state, Consultant) {
var vm = this;
vm.consultants = [];
vm.loadAll = function() {
Consultant.query(function(result) {
vm.consultants = result;
});
};
vm.loadAll();
}
})();
If I inject the $uibModalInstance dependency:
(function() {
'use strict';
angular
.module('myApp')
.controller('ConsultantController', ConsultantController);
ConsultantController.$inject = ['$uibModalInstance','$scope', '$state', 'Consultant'];
function ConsultantController ( $uibModalInstance, $scope, $state, Consultant) {
var vm = this;
vm.consultants = [];
vm.loadAll = function() {
Consultant.query(function(result) {
vm.consultants = result;
});
};
vm.loadAll();
vm.clearSearchDialog = function() {
$uibModalInstance.dismiss('cancel');
};
}
})();
I am getting the following error :
angular.js:13294 Error: [$injector:unpr] Unknown provider: $uibModalInstanceProvider <- $uibModalInstance <- ConsultantController
http://errors.angularjs.org/1.5.2/$injector/unpr?p0=%24uibModalInstanceProvider%20%3C-%20%24uibModalInstance%20%3C-%20ConsultantController
at http://localhost:8181/bower_components/angular/angular.js:68:12
at http://localhost:8181/bower_components/angular/angular.js:4418:19
at Object.getService [as get] (http://localhost:8181/bower_components/angular/angular.js:4571:39)
at http://localhost:8181/bower_components/angular/angular.js:4423:45
at getService (http://localhost:8181/bower_components/angular/angular.js:4571:39)
at injectionArgs (http://localhost:8181/bower_components/angular/angular.js:4595:58)
at Object.instantiate (http://localhost:8181/bower_components/angular/angular.js:4637:18)
at $controller (http://localhost:8181/bower_components/angular/angular.js:9912:28)
at http://localhost:8181/bower_components/angular-ui-router/release/angular-ui-router.js:4081:28
at invokeLinkFn (http://localhost:8181/bower_components/angular/angular.js:9525:9) <div class="well ng-scope" ui-view="content">
But in my project there are several controllers with this dependency.
I don't understand why I have this error on this controller.
[UPDATE]
app.js :
(function() {
'use strict';
angular
.module('myApp', [
'ngStorage',
'ngResource',
'ngCookies',
'ngAria',
'ngCacheBuster',
'ngFileUpload',
'ui.bootstrap',
'ui.bootstrap.datetimepicker',
'ui.router',
'infinite-scroll',
'angular-loading-bar'
])
.run(run);
run.$inject = ['stateHandler'];
function run(stateHandler) {
stateHandler.initialize();
}
})();
working controller :
(function() {
'use strict';
angular
.module('myApp')
.controller('ConsultantDeleteController',ConsultantDeleteController);
ConsultantDeleteController.$inject = ['$uibModalInstance', 'entity', 'Consultant'];
function ConsultantDeleteController($uibModalInstance, entity, Consultant) {
var vm = this;
vm.consultant = entity;
vm.clear = function() {
$uibModalInstance.dismiss('cancel');
};
vm.confirmDelete = function (id) {
Consultant.delete({id: id},
function () {
$uibModalInstance.close(true);
});
};
}
})();
[UPDATE2 ]
.state('consultant.search', {
parent: 'consultant',
url: '/search',
data: {
authorities: ['ROLE_USER']
},
onEnter: ['$stateParams', '$state', '$uibModal', function($stateParams, $state, $uibModal) {
$uibModal.open({
templateUrl: 'app/entities/consultant/consultant-search-dialog.html',
controller: 'ConsultantController',
controllerAs: 'vm',
size: 'md',
}).result.then(function() {
$state.go('consultant', null, { reload: true });
}, function() {
$state.go('^');
});
}]
});

Angularjs service undefined

Was building out an angular application, but am getting error undefined with the service or factory. Specifically "can not read property of setName undefined". I have went here to figure out my error. Even changed around my syntax figuring I maybe had something out of place but still throwing an undefined error.
Here is the controller:
(function() {
"use strict";
angular
.module("app", [])
.controller("mainCtrl", ["$scope", mainCtrl]);
function mainCtrl($scope, productService) {
$scope.testService = productService.setName("Data Service");
}
}());
Here is the service script:
(function() {
"use strict";
angular
.module("app")
.service("productService", function() {
var serviceObj = {};
serviceObj.name = '';
serviceObj.setName = function(newName) {
serviceObj.name = newName;
};
return serviceObj;
});
}());
Originally, I had the service as :
(function() {
"use strict";
angular
.module("app")
.service("productService", setName);
function setName(newName) {
var serviceObj = {};
serviceObj.name = '';
serviceObj.setName = function(newName) {
serviceObj.name = newName;
};
return serviceObj;
}
}());
I've had this problem recently and it seems to be that the function must be defined before the controller is created. I would recommend doing it the following way:
(function () {
"use strict";
//function
function mainCtrl($scope, productService) {
$scope.testService = productService.setName("Data Service");
}
//Controller and Module Init
angular
.module("app", [])
.controller("mainCtrl", mainCtrl);
//Inject requirements (This is needed for Minification)
mainCtrl.$inject = ["$scope", "productService"];
}());

Angularjs error issue

I am getting an error TypeError: Cannot read property 'get' of undefined.i have tried so many ways but not able to solve.
mycode is login.controller.js
(function () {
'use strict';
angular
.module('app')
.controller('LoginController', LoginController);
LoginController.$inject = ['$location', 'AuthenticationService', 'FlashService'];
function LoginController($location, AuthenticationService, FlashService) {
var vm = this;
vm.login = login;
(function initController() {
// reset login status
AuthenticationService.ClearCredentials();
})();
function login() {
var usename=vm.username;
var password=vm.password;
vm.dataLoading = true;
$http.get('http://localhost:8080/ProjectManagement/REST/Login/Check?usename='+usename+'&password='+password+'').success(function(data, status, headers, config,response){
if (data=="0")
{
}
else
{
}
}).error(function(data, status, headers, config,response) {
});
};
}
})();
You missed to add $http dependency inside you controller, make sure all the dependencies are injected before using it.
Code
LoginController.$inject = ['$location', 'AuthenticationService', 'FlashService', '$http'];
function LoginController($location, AuthenticationService, FlashService, $http) {

Resources