我的名字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>
什么问题?
'$ http.get'是异步的,这是行不通的。你需要遵守诺言。 – MinusFour
http://stackoverflow.com/questions/13088153/how-to-http-synchronous-call-with-angularjs这也是 –
附注:您正在定义全局变量GetOrders和GetTotalValueOfProducts,但我认为您打算定义本地功能。 (1)插入'var'关键字:'var GetOrders = function(){...',_or_(2)使用函数声明符号:'function GetOrders(){...' –