After developing my Angular application, I added some basic code to my controller which is displayed below. Now, I am attempting to include two services that I created in my services.js file. This file is being loaded in my index.html and required within my controllers.js file. Strangely, I am unable to access the namespaces passed in, and I cannot seem to identify the reason behind this issue. Despite not encountering any errors and following tutorials with similar setups, I am puzzled by this situation. Any suggestions or solutions would be greatly appreciated. Thank you :)
Controllers.js
angular.module('starter.controllers', ['services'])
.controller('AppCtrl', function($scope, $ionicModal, $timeout, Auth, socket) {
// Form data for the login modal
$scope.loginData = {};
debugger // i dont have access to Auth or socket
// Create the login modal that we will use later
$ionicModal.fromTemplateUrl('templates/login.html', {
scope: $scope
}).then(function(modal) {
$scope.modal = modal;
});
// Triggered in the login modal to close it
$scope.closeLogin = function() {
$scope.modal.hide();
};
// Open the login modal
$scope.login = function() {
$scope.modal.show();
};
// Perform the login action when the user submits the login form
$scope.doLogin = function(user) {
console.log('Doing login', $scope.loginData);
debugger // I dont have access to Auth or socket
// Simulate a login delay. Remove this and replace with your login
// code if using a login system
$timeout(function() {
$scope.closeLogin();
}, 1000);
};
})
Services.js
var baseUrl = 'http://localhost:3000';
angular.module('services', [])
.factory('socket', function socket($rootScope) {
var socket = io(baseUrl);
return {
on: function(eventName, cb) {
socket.on(eventName, function() {
var args = arguments;
$rootScope.$apply(function () {
cb.apply(socket, args);
});
});
},
emit: function(eventName, data, cb) {
socket.emit(eventName, data, function() {
var args = arguments;
$rootScope.$apply(function () {
if (cb) cb.apply(socket, args);
});
});
}
};
})
.factory('Auth', function Auth($q, $http) {
var user = null;
var login = function login (name, password) {
var defer = $q.defer();
var url = baseUrl + '/login';
var postData = { name: name, password: password };
$http.post(url, postData).success(function(response) {
if (response.success && response.success === true) {
user = { name: response.name, id: response.id };
window.localStorage.setItem('user', JSON.stringify(user));
return defer.resolve(response);
} else {
return defer.resolve('No user found');
}
}).error(function(err) {
defer.reject(err);
});
return defer.promise;
}
var currentUser = function currentUser (user) {
return user;
}
var logout = function logout () {
user = null;
window.localStorage.removeItem('user');
}
return {
login: login,
logout: logout,
currentUser: currentUser
};
});