0

In Angular, what is the best practice for triggering code after multiple models have been populated by resource services. Nest $scope.$watch?

Right now, I'm cheating and checking off values in an array, which doesn't feel very "angular."

$scope.loaded = [];
$scope.modelA = aResource.query({}, function() {$scope.loaded.push('a')});
$scope.modelB = bResource.query({}, function() {$scope.loaded.push('b')});

$scope.$watch(loaded.length, function(newValue) {
    if ($scope.loaded.indexOf(modelA) != -1 && $scope.loaded.indexOf(modelB) != -1) {
        console.log('done!'); 
    } 
});

https://groups.google.com/forum/?fromgroups=#!topic/angular/TizlifUL7FU

1 Answer 1

2

If you are using Angular routing, this is normally accomplished with the resolve parameter of the when() method. See also Delaying AngularJS route change until model loaded to prevent flicker

If you don't want to delay your route change until the data is loaded, you can set up your own promises using $q. Use $q.all() to wait for all promises to resolve. See https://stackoverflow.com/a/15117739/215945 and https://stackoverflow.com/a/14545803/215945

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.