Angularjs 然后声明不在角度承诺中运行

Angularjs 然后声明不在角度承诺中运行,angularjs,Angularjs,在我的应用程序中,用户单击一个按钮,该按钮将触发runsOnClick。在runsOnClick中,我尝试使用一个承诺来调用someFunc(这种情况按预期发生),一旦someFunc完成运行,我希望runsOnClick中的这段代码(承诺的部分)运行,但它永远不会运行 then(function() { console.log("in runsOnClick");//this never runs, why? }); 我如何构造一个承诺,使runs中的所

在我的应用程序中,用户单击一个按钮,该按钮将触发
runsOnClick
。在
runsOnClick
中,我尝试使用一个承诺来调用
someFunc
(这种情况按预期发生),一旦
someFunc
完成运行,我希望
runsOnClick
中的这段代码(承诺的
部分)运行,但它永远不会运行

then(function() {
          console.log("in runsOnClick");//this never runs, why?
         });
我如何构造一个承诺,使
runs中的所有代码单击
运行

  $scope.someFunc = function(){
    console.log("in someFunc");
    $http.get('http://localhost:8090/endpoint').success(function(data){
      console.log(data, "this logs");
      });
  },
  $scope.runsOnClick = function(){

     $scope.someFunc().then(function() {
      console.log("in runsOnClick");//this never runs, why?
     });
  },

您缺少返回语句:

  $scope.someFunc = function(){
    console.log("in someFunc");
    //  v----------------------------- here 
    return $http.get('http://localhost:8090/endpoint').success(function(data){
      console.log(data, "this logs");
    });
  },

您尝试调用
.then()
可能失败,因为
$scope.someFunc()
的返回值是
未定义的

您需要
返回
一些内容,以便下一个
运行:

  return $http.get('http://localhost:8090/endpoint').success(function(data){
      console.log(data, "this logs");
  });

$q
不应该因
类型错误而失败,它只会将
未定义的
传递给以下
然后
回调。@davintroon
未定义。然后(…)
将因
类型错误而失败,因为
未定义的
没有
然后
方法。错误不会来自$q.Ah,尽管您指的是
next
undefined
作为回调参数。有道理。