AngularJS-成功返回$ http.post [英] AngularJS - Return in success $http.post

查看:109
本文介绍了AngularJS-成功返回$ http.post的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用AngularJs,我需要在$ http.post连接后返回数据.这是代码:

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:如何将数据传递到myData? -感谢您的答复,对不起,如果我没有解释清楚.

OLD: How can I pass the data into myData? - Thanks for your answers and sorry if I haven't explained well.

但是现在,在html中,{{myData}}我能写些什么?

But now, in html, {{myData}} what can I write?

我不知道它是否是基本的,但是我必须将"myData"和"ng-if"一起使用..

I do not know if it's fundamental, but I'll have to use "myData" with an "ng-if"..

谢谢!我认为我已经解决了! :D

Thanks! I think that I've solved! :D

推荐答案

通常,在您的服务中,您将兑现承诺

Typically in your service you will return the promise

myService.getCheckLogin = function {
    return $http.post("server/checklogin.php",
      {id: "id",email: "email",password: "password"}
      );
}

然后在控制器中

myService.getCheckLogin()
.success(function(data){
  $scope.myData = data;
}
.error(function(err){
  console.log(err);
}

这篇关于AngularJS-成功返回$ http.post的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆