I am trying to display the data to my view but $scope.plan outputs {}. I am thinking that it would output the fetched data from the initGetEdit function, console.log() inside the $http.post outputs expected data.
controller.js
var id = $stateParams.id;
$scope.plan = {}
$scope.initGetEdit = function(){
var data = { id : id }
$http.post("someUrl", data).then(function(res){
$scope.plan = res.data;
console.log($scope.plan); //----> logs expected data
})
}
$scope.initGetEdit();
console.log($scope.plan); //----> logs {}
In my view I have something like this.
view
<input ng-model="plan.something" type="text" />
UPDATE
First thank you for those answers provided and the comments, appreciated it. It gives me an insight. I solved my issue by removing the initGetEdit function and staying with just the http.post.
$http.postis asynchronous, which means the UI does not wait for it to complete before continuing to the next statement. Therefore, theconsole.log()at the end is reached before the data from the server is returned. Angular fires a$digestafter the data is changed, which updates the view, but that has no effect on theconsole.logcalls.postis async,console.log($scope.plan);at the page end would print empty object. But after sometime, your view would be updated.