I have the following angularjs code:
$scope.clients = commonFactory.getData(clientFactory.getClients());
if ($scope.clients.length > 0) {
$scope.sampleForm.ClientId = $scope.clients[0].ClientId;
}
And the getData function in commonFactory:
factory.getData = function (method) {
method.then(function (response) {
return response.data;
}, function (error) {
$rootScope.alerts.push({ type: 'error', msg: error.data.ExceptionMessage });
});
};
The problem is that $scope.clients.length is undefined when it hits that line because of the async call.
Is there a way to not do my length check until I know that $scope.clients has been assigned? I've looked at something like this:
$scope.clients = commonFactory.getData(clientFactory.getClients()).then(function () {
if ($scope.clients.length > 0) {
$scope.sampleForm.ClientId = $scope.clients[0].ClientId;
}
});
Trying to chain my then
promises, but no dice... the goal here is to have the getData method to avoid a bunch of boilerplate code for catching errors... maybe I'm going about this wrong?