AngularJS控制器等待响应(或设置回调) [英] AngularJS Controller wait for response (or set callback)
问题描述
我有一个带有controllers.js和factories.js的AngularJS应用程序。
我喜欢用控制器中的值做些什么(我得到来自工厂)。我的问题是,当我访问它们时,这些值是空的。
我该如何等待响应?或者我可以在哪里添加回调?
pre $
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',函数($ 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;
});
我喜欢的是获得第一辆车($ scope.cards [0])并保存在$ scope.card下。但它总是空的(与cardsLength一样)。另一方面,如果我用局部视图(cards.length)打印大小,我会得到正确的价值。
感谢和感谢
Marc
您可以通过访问值来取回承诺。$ then(必须到源代码中找到):
Flashcards ('CardDeckDetailController',函数($ 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:$然后返回http响应
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
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
这篇关于AngularJS控制器等待响应(或设置回调)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!