AngularJS Controller wait for response (or set callback)

angularjs, html, javascript

Solution

You can get back the promise by accessing value.$then (has to go to the source code to find that):

Flashcards.controller('CardDeckDetailController', function($scope, $routeParams, CardDeckService, CardService) {
$scope.carddeck = CardDeckService.findCardDeckByID($routeParams.cardDeckID);
$scope.cards = CardService.findCardsByCardDeckID($routeParams.cardDeckID);
$scope.card =  $scope.cards.$then(function(){ return $scope.cards[0]; });
$scope.cardLength = $scope.cards.$then(function(){ return $scope.cards.length; });

edit: $then return the http response

Problem

I have a AngularJS application with a controllers.js and factories.js. What I like is to do something with the values in the controller (which I get from the factories). My problem is, that these values are empty at the time I access them. How can I wait for the response? Or where can I add a callback? ``` Flashcards.controller('CardDeckDetailController', function($scope, $routeParams, CardDeckService, CardService) { $scope.carddeck = CardDeckService.findCardDeckByID($routeParams.cardDeckID); $scope.cards = CardService.findCardsByCardDeckID($routeParams.cardDeckID); $scope.card = $scope.cards[0]; $scope.cardLength = $scope.cards.length; }); Flashcards.factory('CardDeckService', function($resource) { var cardDeckService = $resource('/FlashcardsServer/rest/carddecks/:cardDeckID', {}, {}); cardDeckService.findAllCardDecks = function() { return cardDeckService.query(); }; cardDeckService.findCardDeckByID = function(id) { return cardDeckService.get({ cardDeckID : id }); }; return cardDeckService; }); ``` What I like is to get the first car ($scope.cards[0]) and save it under $scope.card. But its always empty (same with cardsLength). On the other hand if I print out the size from the partial view with (cards.length), I get the correct value. Greets and Thanks Marc

Original source

Related problems