I am using a custom directive and trying to capture the $scope.model and if it has initial value in it, I want to process that data etc.
My initial code was:
if ($scope.model && $scope.model.length > 0) {
var id = angular.isArray($scope.model) ? $scope.model.join(',')
: $scope.model;
if (id)
initialJobs.push(
$http.get(config.root.api + 'sectors?id=' + id)
);
}
Because $scope.model is null this if statement doesnt get processed. (I've put a console.log and saw that $scope.model was null).
If I have a timeout of 1000 ms, I capture the real data of $scope.model.
PS: $scope.model data is gathered through a HTTP request in controller and because of the two-way binding, it is first sent to view as null and when the data is formed, model value changes.
Thank you for reading :)
PS: I know that one solution to solve this problem is to resolve the async request in router layer, and than pass it to controller, where there won't be any delay in two-way binding. But I think it is a bad practice for this piece of problem.