Reputation: 873
I use AngularJs and I need to return a data after a $http.post connection. This is the code:
app.controller('PrincipalController',['$http', '$scope', function($http,$scope){
$scope.myData = 0;
$http.post("server/checklogin.php",
{id: "id",email: "email",password: "password"}
)
.success(function(data){
console.log($scope.myData) //0
$scope.myData = data;
console.log($scope.myData); // 1
}
OLD: How can I pass the data into myData? - Thanks for your answers and sorry if I haven't explained well.
But now, in html, {{myData}}
what can I write?
I do not know if it's fundamental, but I'll have to use "myData" with an "ng-if"..
EDIT: Thanks! I think that I've solved! :D
Upvotes: 3
Views: 27093
Reputation: 49185
Typically in your service you will return the promise
myService.getCheckLogin = function {
return $http.post("server/checklogin.php",
{id: "id",email: "email",password: "password"}
);
}
then in controller
myService.getCheckLogin()
.success(function(data){
$scope.myData = data;
}
.error(function(err){
console.log(err);
}
Upvotes: 7
Reputation:
you should do something like this:
.success(function(data){
$scope.myData = data;
//OR
myCallback(data);
console.log(data) //data = 1 (server works);
}
You cannot put the assignment right after the post call, because the call is asynchronous, you cannot predict the exact moment when it will be returning.
I used the $scope
, because normally you might use it through a service on a controller where a scope is available. Otherwise you can use a function in a callback
Upvotes: 2