I am currently working on a project using AngularJS where I am implementing animations for state transitions. To help with this process, I have been referring to the angular wiki and found this Plunker example:
http://plnkr.co/edit/NsZhDL?p=preview
My issue arises when animating between two <table>
elements in different states. The animation (a slide right effect) works correctly, but it triggers before the data is loaded. This results in only the headers sliding in from the right initially, followed by the data being loaded into the <tr>
rows. My controller includes an AJAX call to fetch the necessary data, which seems to be executing after the animation. Is there a way to ensure that the data is loaded prior to triggering the animation?
function CategoryList(/*dependencies*/) {
var controller = this;
//setup stuff
//get the data
$http.get('/My/Url', {
headers: {
ShowLoading: true
}
}).success(function (data, status, headers, config) {
controller.setCategories(data);
});
To address this issue, I attempted using the resolve property within my state object to retrieve the data first and return a promise, but encountered the same problem. Here is my attempt:
.state('myStateName', {
url: '/myUrl/{id:int}',
templateUrl: '/mytemplate.html',
controller: 'ListController',
controllerAs: 'List',
resolve: {
ajaxData: ['$q', '$http', '$stateParams', '$timeout',
function ($q, $http, $stateParams, $timeout) {
var deferred = $q.defer();
// $http returns a promise for the url data
$http.get('/Admin/Activities/Category/GetDetail/' + $stateParams.id, {
headers: {
ShowLoading: false
}
}).success(function (data) {
deferred.resolve(data);
}).error(function (data) {
deferred.reject();
});
return deferred.promise;
}]
}
Even though the data retrieval is successful, the animation still precedes it. I had expected the resolve function to execute before instantiating the controller as per the information provided on the angular-ui wiki page: https://github.com/angular-ui/ui-router/wiki#resolve
edit Further research led me to believe that the AJAX request completes before the animation, but the model binding takes place afterwards. This appears to be the current sequence of events:
- Click on state transition link
- Resolve function retrieves data from server
- Fetch destination state's template
- Animate the new template into position ([ui-view]-enter yada yada)
- Bind the new template to the model.
edit Upon further investigation, it turned out to be a CSS-related issue. The minimum height set on the parent div of the table was causing the tbody content to remain hidden until the animation was completed.