Javascript angular js$http返回true或false

Javascript angular js$http返回true或false,javascript,angularjs,http,return,promise,Javascript,Angularjs,Http,Return,Promise,我在angular的服务中有以下代码: this.login = function(email, password) { var promise = $http({ url: 'user/authenticate', method: "POST", data: { 'email' : email, 'password' : password } }); promise.then( function(payl

我在angular的服务中有以下代码:

this.login = function(email, password)
{

    var promise =  $http({
        url: 'user/authenticate',
        method: "POST",
        data: { 'email' : email, 'password' : password }
    });

    promise.then(
      function(payload) {
         return true;
        }
      );
}
这是我的电话号码:

var loginResult = loginService.login(data.email, data.password);
        alert(oginResult);
        if (loginResult)
        {
            $modalInstance.close("");
            $state.go("home");
        }
        else
        {
            $scope.error = "Login not found.";
        }
我正在尝试返回到调用的service.login。然而,我得到的是未定义的


如果您有任何帮助,我们将不胜感激。您需要更新代码。从服务中回报承诺,并在中完成您的工作。然后在控制器中完成您的工作

服务

this.login = function(email, password)
{

    var promise =  $http({
        url: 'user/authenticate',
        method: "POST",
        data: { 'email' : email, 'password' : password }
    });

    return promise;
}
控制器

loginService.login(data.email, data.password).then(function(loginResult){
        alert(loginResult);
        if (loginResult)
        {
            $modalInstance.close("");
            $state.go("home");
        }
        else
        {
            $scope.error = "Login not found.";
        }


});
  • 你需要在你的服务中做出承诺

    this.login = function(email, password)
    {
      // we directly return $http as $http is a promise.
      return  $http({
          url: 'user/authenticate',
          method: "POST",
          data: { 'email' : email, 'password' : password }
      });
    }
    
  • 将承诺响应管理到控制器中

       // manage the promise return with .then(function() and the response with the parameter of the function
    
    loginService.login(data.email, data.password).then(function(loginResult){
    
        if (loginResult)
        {
            $modalInstance.close("");
            $state.go("home");
        }
        else
        {
            $scope.error = "Login not found.";
        }
    
    
    });
    

  • 好的,我希望在服务中履行承诺,然后返回true或false,这样我在服务之外的代码就可以看起来整洁了。