I have the following code in my javascript controller. The functions work when called independently and asynchronously from the view. However I want to call them synchronously on page load as the returning value from the first function is used in the call for the second function
$scope.function1= function () {
$http({
url: '/Class/method1/',
method: 'GET'
}).success(function (data) {
$scope.mygrid= data.data;
$scope.myvalue= $scope.mygrid[0];
});
};
$scope.function2= function () {
$http({
url: '/class/method2/',
method: 'POST',
params: { myValue: $scope.myvalue }
}).success(function (data) {
$scope.myValue2 = data.data;
});
};
var initialize = function () {
var defer = $q.defer();
defer.promise
.then(function() {
$scope.function1();
})
.then(function() {
$scope.function2();
})
defer.resolve();
};
initialize();
On the second call $scope.myvalue is null. Data has been returned from function one so the only thing I can think of is that function2 is being called too early. Any pointers? :-)