I am new to angularJS. I am trying to seperate service into a seperate file in my application. Everything works fine, but when i try to inject the factory object to the controller it is not working. Below is the code
here is the app file
app.js
'use strict';
var app = angular.module('myApp', ['ngRoute']);
app.config(function ($routeProvider, $controllerProvider,
$compileProvider, $filterProvider, $provide) {
app.controllerProvider = $controllerProvider;
app.compileProvider = $compileProvider;
app.routeProvider = $routeProvider;
app.filterProvider = $filterProvider;
app.provide = $provide;
app.routeResolver = function (basePath, viewName,controllerName, isSecure) {
var routeConf =
{
templateUrl: 'Views/' + basePath + viewName + '.html',
controller: controllerName + 'Controller',
secure: isSecure ? isSecure : false,
resolve: {
deps: function ($q, $rootScope) {
var deferred = $q.defer();
var dependencies =
[
'Controllers/' + basePath + controllerName + 'Controller.js',
];
require(dependencies, function () {
$rootScope.$apply(function () {
deferred.resolve();
});
});
return deferred.promise;
}
}
}
return routeConf;
}
$routeProvider
.when('/', {})
.when('/Admin', app.routeResolver('Admin/', 'Admin','Admin'))
.when('/Customer', app.routeResolver('Customer/', 'Customer','Customer'))
.when('/Teller', app.routeResolver('Teller/', 'Teller','Teller'))
.when('/Finance', app.routeResolver('Finance/', 'Finance','Finance'))
.otherwise({ redirectTo: '/' });
});
Here is my factory
AuthenticationService.js
'use strict';
app.factory('authenticationService', ['$scope', 'httpService', function ($scope, httpService) {
return {
authenticateUser: function () {
if (!$scope.userName && !$scope.keyWord) {
var result = httpService.postService(
{
username: $scope.userName,
keyword: $scope.keyWord
}, "../api/authenticateUser");
result.then(successCall, errorCall);
}
}
}
successCall =function(dataObject) {
}
errorCall = function (dataObject) {
}
}]);
Here is my controller
LoginController.js
'use strict';
app.controller('LoginController', ['$scope','authenticationService', function ($scope, authenticationService) {
$scope.ValidateLogin = function () {
var result = window.confirm("Validate Login Called for the user :" + $scope.userName);
var result1 = authenticationService.authenticateUser();
}
}]);
the logincontroller is working fine when i am not injecting the authenticationservice. But when i try to inject authenticationservice, it is not working. what am i doing worng. Your help will be greatly appreciated. Thanks in advance.
Note : I have tried creating this as a service as well. Still this is not working.