Hey there! I'm new to AngularJS and I'm trying to figure out how to pass my empty $scope to my $factory for dynamic login functionality. I'm using $http to retrieve data from my API and I attempted to assign the scope in the factory, but it didn't work as expected.
Here's my serviceAPI:
(function() {
"use strict";
angular.module('starter').factory(serviceAPI', function($http, $q, $ionicLoading, $timeout) {
function getData() {
var deferred = $q.defer();
$ionicLoading.show({ template: 'Loading...' });
var url = "myAPI";
var data = {
username: "admin", <-- I want to make this dynamic
password: "admin" <--
};
$http({
method: 'POST',
url: url,
data: data
}).success(function(data) {
$ionicLoading.hide();
deferred.resolve(data);
}).error(function() {
console.log('Error while making HTTP call');
$ionicLoading.hide();
deferred.reject();
});
return deferred.promise;
}
// Return value to public
return {
getData: getData,
};
})
}());
This is the controller:
(function() {
"use strict";
angular.module('starter').controller('LoginCtrl', ['$scope', 'serviceAPI', LoginCtrl]);
function LoginCtrl($scope, serviceAPI) {
$scope.username = "";
$scope.password = "";
$scope.login = function() {
serviceAPI.getData().then(function(data) {
console.log(data)
});
}
}
}());