I have been dealing with some older code that utilizes angularjs 1.x for a web frontend. I am in the process of creating a modal dialog that needs to make a RESTful call to the backend upon opening and wait for the data to be returned before displaying the view.
Most of the steps were clear to me, but there is one aspect that is confusing me. I initially thought I needed to use 'resolve' to define a function that would return a $promise to the controller. However, when I checked inside my controller using a breakpoint, I discovered that the parameter is an object containing the promise, resolution status, and then the actual data.
Although I can extract the necessary data from this object, it seems unnecessary. My focus in the controller is solely on the data that is returned, not the promise itself. Is there a way to structure this so that only the data is passed to the controller, or is this simply how angular modals are designed to work?
A snippet of the code I am working with:
$scope.openTerritorySelect = function () {
var modalInstance = $modal.open({
animation: true,
templateUrl: 'prospect/detail/selectTerritoriesModal.tpl.html',
controller: function($scope, $modalInstance, availableReps){
$scope.reps = availableReps;
$scope.ok=function()
{
$modalInstance.close();
};
$scope.cancel=function()
{
$modalInstance.dismiss('cancel');
};
},
resolve: {
availableReps: function () {
return Prospect.getRelatedReps({}, function (data, header) {
$scope.busy = false;
return data.result;
}, function (response) {
$scope.busy = false;
if (response.status === 404) {
$rootScope.navError = "Could not get reps";
$location.path("/naverror");
}
}).$promise;
}
}
});
modalInstance.result.then(function (selectedReps) {
}, function () {
console.log('Modal dismissed at: ' + new Date());
});
};
The 'Prospect' service class:
angular.module('customer.prospect', [ "ngResource" ]).factory('Prospect', [ 'contextRoute', '$resource', function(contextRoute, $resource) {
return {
getRelatedReps : function(args, success, fail) {
return this.payload.getRelatedReps(args, success, fail);
},
payload : $resource(contextRoute + '/api/v1/prospects/:id', {
}, {
'getRelatedReps' : {
url : contextRoute + '/api/v1/prospects/territories/reps',
method : 'GET',
isArray : false
}
})
};
} ]);