Reputation: 15
I am using angular in saleforce VF and getting data from apex controller.
Below is the concerned code:
function Quote_Details_Ctrl($scope,quoteDetailsFactory){
console.log('Inside Angular Controller');
$scope.quoteData = [];
$scope.quoteId = "{!$CurrentPage.parameters.quoteId}";
console.log('quoteId : ' + $scope.quoteId);
$scope.loadData = function(){
var promise = quoteDetailsFactory.loadData($scope.quoteId);
console.log('promise : ' ,promise);
promise.then(function(obj){
$scope.quoteData = obj;
console.log('obj : ' ,obj);
},[]);
console.log('quoteData : ',$scope.quoteData);
}
console.log('Hi Controller ');
$scope.loadData('quoteData2 : ',$scope.quoteData);
}
when I print obj in console, it prints the data correctly. However, when I assign it to the scope variable and console the scope variable, it shows as an empty object with no data.
Can someone please tell if something is missing.
Thanks
Upvotes: 0
Views: 116
Reputation: 25
This is happening because the time when the statement console.log('quoteData : ',$scope.quoteData);
executes, the promise hasn't yet resolved.
What you can do is either access this variable from the .then()
function only, or you can call another function when this promise is resolved
promise.then(function(obj){
$scope.quoteData = obj;
console.log('obj : ' ,obj);
$scope.helperFunc(obj);
},[]);
$scope.helperFunc(quoteData) {
console.log("Here you can get the quoteData: " , quoteData)
}
Upvotes: 0
Reputation: 1004
$scope.quoteData
has to be accessed within the promise resolve function
$scope.loadData = function(){
var promise = quoteDetailsFactory.loadData($scope.quoteId);
console.log('promise : ' ,promise);
promise.then(function(obj){
$scope.quoteData = obj;
console.log('obj : ' ,obj);
console.log('quoteData : ',$scope.quoteData);
},[]);
}
Upvotes: 1