I have a factory called "Server" that contains my methods of interacting with the server (get / put / post / delete ..). I managed to log in and get all the data when I had all my code in the controller. Now that I want to split this code and rebuild it a bit, I am running into problems. I can still log in and I also receive data, but the data is just printed; I'm not sure how to access the data in the controller? I saw some “. Then” instead of “.success” used here and there over the Internet, but I don’t know how.
This is my factory: (included in services.js)
app.factory('Server', ['$http', function($http) {
return {
login: function(email,pass) {
return $http.get('mywebapiurl/server.php?email='+email+'&password='+pass').success(function(data) {
console.log("\nLOGIN RESPONSE: "+JSON.stringify(data));
if(data.Status !== "OK")
// login fail
console.log("Login FAIL...");
else
// success
console.log("Login OK...");
});
},
// intentional blank data parameter below (server configured this way for testing purposes)
getAllData: function() {
return $http.get('mywebapiurl/server.php?data=').success(function(data) {
console.log("\nDATA FROM SERVER: \n"+data); // here correct data in JSON string format are printed
});
},
};
}]);
This is my controller:
app.controller("MainController", ['$scope', 'Server', function($scope, Server){
Server.login();
$scope.data = Server.getAllData();
…. continues …
, $http factory ? .
, , . , , , ?
EDIT: factory ng-click, . :
$scope.updateContacts = function(){
$http.get('mywebapiURL/server.php?mycontacts=').success(function(data) {
$scope.contacts = data;
});
};
ng-click = "updateContacts()". , $scope.contacts . .then? ( )
:
, , ( ), , . AngularJS? , , ...