2015-11-22 39 views
-1

我的名字tst控制器中定义一个变量,我给在由名称GetOrders函数一定的价值,但是当我在另一个函数GetTotalValueOfProducts得到tst它是不确定的,没有价值,这是我做的:传递两个函数之间的变量在同一个控制器

<script> 
app.controller('OrderController', function ($scope, $http) { 
    $scope.quantity = 1; 
    $scope.Orders = {}; 
    var tst ; 
    GetOrders = function() { 
     $http.get('/Order/GetAllOrders').success(function (response) { 
      $scope.Orders = response; 
      //here i set tst, and tst has value, i checked it has value and it's not undefined 
      tst = response; 

     }); 
    } 
    GetOrders(); 

    GetTotalValueOfProducts = function() { 
     //but here 'tst' is undefined!!! 
     var p = tst; 

    } 
    GetTotalValueOfProducts(); 
    }); 
</script> 

什么问题?

+1

'$ http.get'是异步的,这是行不通的。你需要遵守诺言。 – MinusFour

+0

http://stackoverflow.com/questions/13088153/how-to-http-synchronous-call-with-angularjs这也是 –

+0

附注:您正在定义全局变量GetOrders和GetTotalValueOfProducts,但我认为您打算定义本地功能。 (1)插入'var'关键字:'var GetOrders = function(){...',_or_(2)使用函数声明符号:'function GetOrders(){...' –

回答

-1

尝试将tst变量附加到您的内部$scope对象中。 编辑 这是sl not,没有看到这些功能是异步的。您应该将结果提取为:

尝试将tst变量附加到您的内部$scope对象中。编辑 这是sl not不看,这些功能是异步的。你应该把你的结果作为。

app.controller('OrderController', function ($scope, $http) { 
    $scope.quantity = 1; 
    $scope.Orders = {}; 
    $scope.tst; 
    GetOrders = function() { 
     $http.get('/Order/GetAllOrders').success(function (response) { 
      $scope.Orders = response; 
      $scope.tst = response; 
      GetTotalValueOfProducts(); 
     }); 
    } 
    GetOrders(); 

    GetTotalValueOfProducts = function() { 
    //but here 'tst' is undefined!!! 
     var p = $scope.tst; 
    } 

}); 

甚至尝试使用$q

+0

问题是功能的异步性质,您的答案无法解决。 –

+0

@IngoBürk感谢您的建议,我已经修复了我的回答 – Theodore

1

这是发生,因为JS是异步的。

您正在执行GetOrders(),因为它正在发出http请求,这需要时间,但在http请求完成之前,您正在调用GetTotalValueOfProducts,这使得它未定义。

<script> 
app.controller('OrderController', function ($scope, $http) { 
    $scope.quantity = 1; 
    $scope.Orders = {}; 
    var tst ; 

    GetTotalValueOfProducts = function() { 
     //but here 'tst' is undefined!!! 
     var p = tst; 

    } 

    GetOrders = function() { 
     $http.get('/Order/GetAllOrders').success(function (response) { 
      $scope.Orders = response; 
      //here i set tst, and tst has value, i checked it has value and it's not undefined 
      tst = response; 
      GetTotalValueOfProducts(); 

     }); 
    } 
    GetOrders(); 



    }); 
</script> 

以上应该会给你预期的结果。不确定何时调用该函数。

1

嗨有AngularJS是所有异步,所以GetTotalValueOfProducts();成功回调之前调用返回任何值从http。一旦回调完成,您必须调用您的函数。

您可以使用$ q的AngularJS来完成它。这里是链接$q

1

这是一个经典的JavaScript异步问题。第二个函数在第一个函数中的promise被解析之前执行。

+0

凭借5k +的声誉,人们会认为您知道什么是“重复”,如何将其标记为如此以及如何为此投票。 –

0

你应该叫

GetTotalValueOfProducts(); 

在$ http服务的成功回调。改变这样的代码

<script> 
    app.controller('OrderController', function ($scope, $http) { 
     $scope.quantity = 1; 
     $scope.Orders = {}; 
     GetOrders = function() { 
     $http.get('/Order/GetAllOrders').success(function (response) { 
     $scope.Orders = response; 
     GetTotalValueOfProducts(response) 
     }); 
     } 
     GetOrders(); 

     GetTotalValueOfProducts = function (tst) { 
     var p = tst; 
     } 

    }); 
</script> 
相关问题