AngularJS控制器等待响应(或设置回调) [英] AngularJS Controller wait for response (or set callback)

查看:277
本文介绍了AngularJS控制器等待响应(或设置回调)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆