I am currently working on retrieving the Balance value of an ethereum account using the web3 API. My goal is to extract this value and store it in the $scope so that I can access it in my HTML. However, I seem to be encountering an issue where I consistently receive a "value is undefined" error. This problem potentially stems from web3 operating asynchronously, although I cannot confirm this as the root cause of the issue. Presented below is the excerpt of my code:
app.controller('mainController', function ($scope) {
$scope.showBalance = function(){
web3.eth.getBalance("0xCc26fda641929192B2Fe96BBc37DB5B451Cb9A8c",
function(err, res){
$scope.balance = res.c[0]
console.log("This is inside:" + $scope.balance);
});
console.log("This is outside:" + $scope.balance);
};
angular.element(document).ready(function () {
$scope.showBalance();
});
});
While the console log output for "This is inside" displays the correct value, indicating that the operation is successful. The second console log referenced by "This is outside" results in an undefined value.